can someone help me with this baffling regex ?

S

Spydo

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)
 
W

Willem

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
 
U

Uri Guttman

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
 
S

Spydo

  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

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..
 
U

Uri Guttman

"W" == Willem  <[email protected]> writes:

  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

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
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top