Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

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.


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

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (2)
As of 2023-03-24 18:29 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (61 votes). Check out past polls.