Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: regex help!

by arturo (Vicar)
on Nov 18, 2003 at 18:50 UTC ( #308075=note: print w/ replies, xml ) Need Help??


in reply to regex help!

Count me as a little bit flummoxed by the pattern. What you're telling perl you want to match is:

an optional literal pipe character followed by something in the following class: (a space, a literal asterisk, a literal |, a literal dot, a literal as +terisk ) followed by a literal pipe character
That is, your pattern looks like this: \|?[ *|\.*]\|

Inside the character class, the *, the |, and the . don't have special meanings, and are interpreted as the literal characters. You appear to be trying to quantify over spaces and periods inside the character class, but that makes no sense (the character class matches any one of the characters in the class).

Secondly, although this isn't the source of your problem, the RHS of the substitution doesn't get interpreted as a regular expression (just as a double-quoted string, with a few differences -- see perlop on s/PATTERN/REPLACEMENT/). So you could just use s/$pattern/||/g there.

What's going wrong -- given your input string -- is that every time a period or space character is followed by a | character (that sequence does match your pattern), two || characters are thrown into the result. When there's only one | in the original -- the first one being optional, remember -- you're putting in two for one.

It appears that what you want to do is collapse the parts of the string between two pipes when the intervening space consists only of a single space or period. s/(\|?)[ .]\|/$1|/g appears to work; it only puts in a second | if one was in the pattern that was initially matched (on each match, $1 contains nothing or it contains a |). Depending on how well-behaved your input is, this should do the job.

HTH

If not P, what? Q maybe?
"Sidney Morgenbesser"


Comment on Re: regex help!
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://308075]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2014-10-25 20:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (149 votes), past polls