Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

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

( #302808=categorized answer: print w/ replies, xml ) Need Help??

Q&A > regular expressions > 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.

Comment on Answer: How do I match for a pattern in an array element delimited with a /
Download Code
Re: Answer: How do I match for a pattern in an array element delimited with a /
by Roy Johnson (Monsignor) on Feb 02, 2004 at 19:14 UTC
    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
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 having an uproarious good time at the Monastery: (13)
As of 2015-07-02 22:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (45 votes), past polls