|No such thing as a small change|
Re: regex help!by arturo (Vicar)
|on Nov 18, 2003 at 18:50 UTC||Need Help??|
Count me as a little bit flummoxed by the pattern. What you're telling perl you want to match is:
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?