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"


In reply to Re: regex help! by arturo
in thread regex help! by Plankton

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.