can someone help me with this baffling regex ?

Discussion in 'Perl Misc' started by Spydo, Feb 8, 2011.

  1. Spydo

    Spydo Guest

    Hello experts:

    Honestly it's very unusual that I can't figure out a tite lil' regex
    to solve a substitution, but this one has me baffled.. And it looks
    simple..

    I'm trying to remove ALL but the last dot in a scalar.

    So I want to replace

    'cat.dog.mouse.eel.txt'

    with

    'catdogmouseeel.txt'

    I've tried a bunch of approaches, and one actually WORKS but has
    warnings...

    WORKS but throws warnings seemingly for every extra dot:

    s/\.(?=[^\.]*\.)/$1/g;

    DB<7>
    Use of uninitialized value in substitution iterator at x.pl line 5.
    at x.pl line 5
    Use of uninitialized value in substitution iterator at x.pl line 5.
    at x.pl line 5
    Use of uninitialized value in substitution iterator at x.pl line 5.
    at x.pl line 5
    DB<7> x
    $_
    0 'catdogmouseeel.txt'


    DOES NOT WORK- Im not really sure why:
    s/([^\.]*)\.?=(.*\.)/$1$2/g;

    (does nothing)
     
    Spydo, Feb 8, 2011
    #1
    1. Advertisements

  2. Spydo

    Willem Guest

    Spydo wrote:
    ) Hello experts:
    )
    ) Honestly it's very unusual that I can't figure out a tite lil' regex
    ) to solve a substitution, but this one has me baffled.. And it looks
    ) simple..
    )
    ) I'm trying to remove ALL but the last dot in a scalar.
    )
    ) So I want to replace
    )
    ) 'cat.dog.mouse.eel.txt'
    )
    ) with
    )
    ) 'catdogmouseeel.txt'
    )
    ) I've tried a bunch of approaches, and one actually WORKS but has
    ) warnings...
    )
    ) WORKS but throws warnings seemingly for every extra dot:
    )
    ) s/\.(?=[^\.]*\.)/$1/g;

    What's that $1 for ? There is no capture.

    (The ?= makes it a postmatch, instead of capturing parens.)


    SaSW, Willem
    --
    Disclaimer: I am in no way responsible for any of the statements
    made in the above text. For all I know I might be
    drugged or something..
    No I'm not paranoid. You all think I'm paranoid, don't you !
    #EOT
     
    Willem, Feb 8, 2011
    #2
    1. Advertisements

  3. Spydo

    Uri Guttman Guest

    W> Spydo wrote:
    W> ) I'm trying to remove ALL but the last dot in a scalar.
    W> )
    W> ) WORKS but throws warnings seemingly for every extra dot:
    W> )
    W> ) s/\.(?=[^\.]*\.)/$1/g;

    W> What's that $1 for ? There is no capture.

    yep.

    W> (The ?= makes it a postmatch, instead of capturing parens.)

    and this is the same thing but simplified and it seems to work:

    echo 'foo.bar.baz.quux' | perl -wlpe 's/\.(?=.*\.)//g'
    foobarbaz.quux

    uri
     
    Uri Guttman, Feb 8, 2011
    #3
  4. Spydo

    Spydo Guest

    Thanks guys

    I think I see what you mean the ?= wipes out the match? Still- I would
    have expected $1 to be just '', but I guess its actually undef.

    I don't use ?= often and when I do it usually doesn't seem to do what
    I expected..
     
    Spydo, Feb 8, 2011
    #4
  5. Spydo

    Uri Guttman Guest

    S> I think I see what you mean the ?= wipes out the match? Still- I would
    S> have expected $1 to be just '', but I guess its actually undef.

    why would it be ''? that is an empty string which could be a real grab
    (just of 0 length). you need $1 to be undef to say it never was set by a
    grab.

    S> I don't use ?= often and when I do it usually doesn't seem to do what
    S> I expected.

    it is very simple. first, it is a zero width assertion which means it
    doesn't eat chars from the data. you use other zero width assertions
    like ^ and \b so this is the same. then it just matches what is at the
    match point now and fails if it doesn't. just combine those two
    properties and it is easy to get and use.

    uri
     
    Uri Guttman, Feb 8, 2011
    #5
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.