Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

How do I match for a pattern in an array element delimited with a /

( #206423=categorized question: print w/ replies, xml ) Need Help??
Contributed by Anonymous Monk on Oct 18, 2002 at 20:43 UTC
Q&A  > regular expressions


Here is the data in one element of an array


I need to do the following:

Match everything between the "/"'s if it starts with /MSS, and delete out the space, unless there is a * after the space.

So my data would then look like this:


Is this possible? If so, could someone show me how?

Edit by tye. Will be moved to Seekers of Perl Wisdom soon.

Answer: How do I match for a pattern in an array element delimited with a /
contributed by nothingmuch

foreach (@array){ # iterate the array while(m#/MSS#g){ # find a /MSS substr($_,pos,do { m#/#g; pos }) =~ s/ (?!\*)//g; # find a closing / # substring the range currently in effect /MSS to / # replace all ' ' (replace with \s if applicable) # not followed by a * with nothing } }
Answer: How do I match for a pattern in an array element delimited with a /
contributed by Roy Johnson

A little more straightforwardly:

foreach (@array) { s{/(MSS.*?)/} { my $m=$1; $m=~s/ +([^*])/$1/g; $m}ge; }
Note that I'm using {} as delimiters for the main s///, and that what looks like an inner block of code is the replacement portion. The e option causes it to be eval'd.

Within the eval section, I operate on the matched string, and replace any spaces that are followed by something other than a * with whatever they are followed by.

Without the +, it would squeeze multiple spaces into one space, when followed by a *, and if not followed by a *, it would (of course) remove them all.

Please (register and) log in if you wish to add an answer

  • 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, 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, 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.
  • Log In?

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

    How do I use this? | Other CB clients
    Other Users?
    Others chanting in the Monastery: (8)
    As of 2015-11-27 10:48 GMT
    Find Nodes?
      Voting Booth?

      What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

      Results (724 votes), past polls