Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Re: Searchin' through values

by ikegami (Pope)
on Dec 14, 2009 at 01:42 UTC ( #812628=note: print w/replies, xml ) Need Help??

in reply to Searchin' through values

If you treat the arg as a regex pattern, "[P/R]" actually means "P", "/" or "R". You simply want "[PR]".

"." means any character

"[QRG]" will match one of those three characters.

And if you want to format you specified:

my $pat = ''; for ($ARGV[0]) { /\G ([A-EG-WYZ]+) /xgc && do { $pat .= $1; redo }; /\G X /xgc && do { $pat .= '.'; redo }; /\G F /xgc && do { $pat .= '[QRG]'; redo }; /\G \[ ([A-Z]) \/ ([A-Z]) \] /xgc && do { $pat .= "[$1$2]"; redo }; /\G \z /xgc && last; my $pos = pos(); my $next = substr($_, $pos, 1); die("Unrecognized character \"$next\" at pos $pos\n"); }

Update: Added missing /xgc.

Replies are listed 'Best First'.
Re^2: Searchin' through values
by vitoco (Friar) on Dec 14, 2009 at 13:08 UTC

    I found very interesting this piece of code as an example for the \G pattern, but I noticed that x modifier was missing on each regexp AFAIK, but then tested if there was anything else I'm not awared.

    Just as it is, it returns nothing on $pat. Then added the x modifier and got an endless loop!

    I thought it was related to a note in perlre: "It is worth noting that \G improperly used can result in an infinite loop.", but a small trace using print "($pat)\n"; just before each redo shows that \G is always at the beginning of the argument, not after what was already matched.

    I tried again removing pos() to see if that function resets the pointer... No luck!

    Am I doing something wrong?

      It's because 'x' wasn't the only missing modifier. 'g' (to make the engine pay attn to pos) and 'c' (to prevent the engine from resetting pos) were also missing. Fixed.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://812628]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2018-04-24 03:47 GMT
Find Nodes?
    Voting Booth?