Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^4: Multiple regex matches in single string

by hipowls (Curate)
on Apr 26, 2008 at 23:33 UTC ( #683096=note: print w/ replies, xml ) Need Help??


in reply to Re^3: Multiple regex matches in single string
in thread Multiple regex matches in single string

That assumes that the starts are on consecutive lines which may be a perfectly valid assumption. It pays to know your data.

Another approach is to use the original regex, which may have multiple starts and then trim it using s/^.*start/start/is.

The loop then looks something like

while ( $string =~ /(start.+?end)/gis ) { my $data = $1; $data =~ s/^.*start/start/is; print $data, "\n\n"; }
If the intent is to strip off multiple starts only on consecutive lines then the regex would be s/^(?:start\s*)+start/start/is which used on the input
start start start go one end start start data start go two end
would produce
start go one end start data start go two end
But as I said you really need to know your data and other factors such as if you need to validate the input.


Comment on Re^4: Multiple regex matches in single string
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://683096]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (11)
As of 2014-12-20 19:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (97 votes), past polls