Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need 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"


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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (6)
As of 2024-04-24 09:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found