Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

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


Description:

Here is the data in one element of an array

D RTVX/TN 890-567-4567/CFDN/LCC 888/MSS *456LSL MSSSSSS/LEFV

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:

D RTVX/TN 890-567-4567/CFN/LCC 888/MSS *445LSLMSSSSSS/LEFV

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!
  • 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
  • Outside of code tags, you may need to use entities for some characters:
            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?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others cooling their heels in the Monastery: (9)
    As of 2014-08-27 12:11 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The best computer themed movie is:











      Results (238 votes), past polls