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

Discussion in 'Perl Misc' started by Sparticus, Mar 9, 2006.

  1. Sparticus

    Sparticus Guest

    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
     
    Sparticus, Mar 9, 2006
    #1
    1. Advertising

  2. "Sparticus" <> 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
     
    Eric Schwartz, Mar 9, 2006
    #2
    1. Advertising

  3. Sparticus

    DJ Stunks Guest

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

    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
     
    DJ Stunks, Mar 9, 2006
    #3
  4. 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
     
    John W. Krahn, Mar 9, 2006
    #4
  5. Sparticus

    Sparticus Guest

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

    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!
     
    Sparticus, Mar 9, 2006
    #5
  6. Sparticus

    DJ Stunks Guest

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

    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
     
    DJ Stunks, Mar 9, 2006
    #6
  7. Sparticus

    DJ Stunks Guest

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

    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
     
    DJ Stunks, Mar 9, 2006
    #7
  8. Re: Non-Greedy. Please help. Need for answer asap!

    "Sparticus" <> 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
     
    Eric Schwartz, Mar 9, 2006
    #8
  9. Sparticus

    S.Marion Guest

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

    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
     
    S.Marion, Mar 9, 2006
    #9
  10. Re: Non-Greedy. Please help. Need for answer asap!

    "S.Marion" <> 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
     
    Eric Schwartz, Mar 9, 2006
    #10
  11. Re: Non-Greedy. Please help. Need for answer asap!

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


    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
     
    Jürgen Exner, Mar 10, 2006
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. chelsy

    Someone please help ASAP

    chelsy, Aug 18, 2003, in forum: Java
    Replies:
    1
    Views:
    358
    Joseph Millar
    Aug 18, 2003
  2. IC Browsers
    Replies:
    1
    Views:
    344
    Roedy Green
    May 10, 2004
  3. jason12
    Replies:
    0
    Views:
    597
    jason12
    Jul 29, 2007
  4. Dan Kelly

    Greedy and non greedy quantifiers

    Dan Kelly, Jan 17, 2008, in forum: Ruby
    Replies:
    4
    Views:
    147
    Robert Klemme
    Jan 19, 2008
  5. Matt Garrish

    greedy v. non-greedy matching

    Matt Garrish, Feb 16, 2004, in forum: Perl Misc
    Replies:
    4
    Views:
    164
    Matt Garrish
    Feb 16, 2004
Loading...

Share This Page