Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Non-Greedy. Please help. Need for answer asap!

Reply
Thread Tools

Non-Greedy. Please help. Need for answer asap!

 
 
Sparticus
Guest
Posts: n/a
 
      03-09-2006
I am a stupid project due tomorrow and I am dying over a simple regular
expression.

I have the string :

$blah = "123;456;789;abc;def;ghi";

and all I want to do is remove anything at the end of the string up to
and include the semi-colon.

so I want to end of with :

$blah = "123;456;789;abc;def";

how is this done? I know I need the nongreedy stuff here... this is
what I tried and nothign seems to work :

$blah =~ s/;.*?$//;
$blah =~ s/\;.*?$//;
$blah =~ s/;(.*)?$//;
$blah =~ s/;?.*$//;
$blah =~ s/(;.*)?$//;

Nothing seems to work.... I need getting empty strings or at best I get
:

$blah = "123";

Anyone? I am going crazy here!....lol

Thanx

 
Reply With Quote
 
 
 
 
Eric Schwartz
Guest
Posts: n/a
 
      03-09-2006
"Sparticus" <(E-Mail Removed)> writes:
> I am a stupid project due tomorrow and I am dying over a simple regular
> expression.


Keep in mind that, in general, USENET is an asynchronous system, so
you can't be sure of any timeliness in responding. I just happened to
catch you, so you're lucky.

> I have the string :
>
> $blah = "123;456;789;abc;def;ghi";
>
> and all I want to do is remove anything at the end of the string up to
> and include the semi-colon.


So what you want to do is, remove the last ';', and everything
following it that's not a ';'.

my $re = qr/ ; # a semicolon
[^;]* # followed by some non-semicolons
$ # followed by the end of the string.
/x;

No need to be greedy (or non-greedy) here. Just re-think your regex.

FWIW, thanks for showing some attempts-- I for one don't mind helping
somebody who's tried something and failed, but I do mind someone who
hasn't tried asking for me to fix his code for him.

-=Eric

 
Reply With Quote
 
 
 
 
DJ Stunks
Guest
Posts: n/a
 
      03-09-2006

Sparticus wrote:
> I am a stupid project


finally, truth in advertising.

you don't need non-greedy. anchor your regex to the end of the line
and pick a metacharacter which _doesn't_ match a ;.

-jp

 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      03-09-2006
Sparticus wrote:
> I am a stupid project due tomorrow and I am dying over a simple regular
> expression.
>
> I have the string :
>
> $blah = "123;456;789;abc;def;ghi";
>
> and all I want to do is remove anything at the end of the string up to
> and include the semi-colon.
>
> so I want to end of with :
>
> $blah = "123;456;789;abc;def";


$ perl -le'
$blah = "123;456;789;abc;def;ghi";
print $blah;
$blah =~ s/;[^;]*$//;
print $blah;
'
123;456;789;abc;def;ghi
123;456;789;abc;def



John
--
use Perl;
program
fulfillment
 
Reply With Quote
 
Sparticus
Guest
Posts: n/a
 
      03-09-2006
Thanx for the quick reply!! Both of your responses worked perfectly
and you saved me from pulling any more hair out. And I learned
something too.

thanx again!

 
Reply With Quote
 
DJ Stunks
Guest
Posts: n/a
 
      03-09-2006

Sparticus wrote:
> I am a stupid project


finally, truth in advertising.

you don't need non-greedy. anchor your regex to the end of the line
and pick a metacharacter which _doesn't_ match a ;.

-jp

 
Reply With Quote
 
DJ Stunks
Guest
Posts: n/a
 
      03-09-2006

Sparticus wrote:
> I am a stupid project


finally, truth in advertising.

you don't need non-greedy. anchor your regex to the end of the line
and pick a metacharacter which _doesn't_ match a ;.

-jp

 
Reply With Quote
 
Eric Schwartz
Guest
Posts: n/a
 
      03-09-2006
"Sparticus" <(E-Mail Removed)> writes:
> Thanx for the quick reply!! Both of your responses worked perfectly
> and you saved me from pulling any more hair out. And I learned
> something too.


To whom are you replying? I don't see any context in your post, so
I'm not sure if my post helped you, or someone else's. Please quote
the relevant bits of the post you're replying to.

-=Eric
 
Reply With Quote
 
S.Marion
Guest
Posts: n/a
 
      03-09-2006
Eric Schwartz wrote:
> To whom are you replying? I don't see any context in your post, so
> I'm not sure if my post helped you, or someone else's. Please quote
> the relevant bits of the post you're replying to.
>
> -=Eric


The problem I find with the newsgroup is that you can't quote more than
what you write (at least I can't)... which seem stupid to me.
So for the context... you can either remove it, or write a novel to be
abled to quote as much as you want.

Sebastien Marion
 
Reply With Quote
 
Eric Schwartz
Guest
Posts: n/a
 
      03-09-2006
"S.Marion" <(E-Mail Removed)> writes:
> Eric Schwartz wrote:
> > To whom are you replying? I don't see any context in your post, so
> > I'm not sure if my post helped you, or someone else's. Please quote
> > the relevant bits of the post you're replying to.
> > -=Eric

>
> The problem I find with the newsgroup is that you can't quote more
> than what you write (at least I can't)... which seem stupid to me.
> So for the context... you can either remove it, or write a novel to be
> abled to quote as much as you want.


Fix your newsreader, then (or get a new one, if you like). I'd look
into that, as gnus lets me quote however much I like. I'm only
quoting as much as I am this time to prove the point.

-=Eric
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Wrong answer equals to a blank answer or not? Zadkin Microsoft Certification 8 06-27-2006 01:51 PM
please help... ...me learn C++ please please please :) KK C++ 2 10-14-2003 02:08 PM
O/T I need a simple woodworking answer please - hardwood floors loomis Computer Support 8 10-03-2003 12:03 PM
PRETTY PLEASE...need answer to why "this method should result in NodeSet" error KathyB ASP .Net 0 07-25-2003 04:59 AM



Advertisments