http://www.perlmonks.org?node_id=206423

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

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.

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: How do I match for a pattern in an array element delimited with a /
by nothingmuch (Priest) on Oct 18, 2002 at 21:03 UTC
    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 } }
Re: How do I match for a pattern in an array element delimited with a /
by Roy Johnson (Monsignor) on Oct 28, 2003 at 20:42 UTC
    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.

      You need to include the slashes in the capture.

      Here's yet another way:

      for (@array) { while (m(/MSS)g) { s{\G([^/]*?) +(?![* ])}{$1}g } }

      The PerlMonk tr/// Advocate