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

S

Sparticus

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
 
E

Eric Schwartz

Sparticus said:
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
 
D

DJ Stunks

Sparticus said:
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
 
J

John W. Krahn

Sparticus said:
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
 
S

Sparticus

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!
 
D

DJ Stunks

Sparticus said:
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
 
D

DJ Stunks

Sparticus said:
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
 
E

Eric Schwartz

Sparticus said:
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
 
S

S.Marion

Eric said:
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
 
E

Eric Schwartz

S.Marion said:
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
 
J

Jürgen Exner

S.Marion said:
The problem I find with the newsgroup is that you can't quote more
than what you write
Nonsense.

(at least I can't)

Then learn how to use your Newsreader (or get a different one)
... which seem stupid to me.

Quite true
So for the context... you can either remove it, or write a novel to be
abled to quote as much as you want.

Not exactly. You can always shorten the quoted text to the relevant part.
Which is a good idea to do anyway.

jue
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,831
Latest member
HealthSmartketoReviews

Latest Threads

Top