Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re: pattern matching

by Athanasius (Chancellor)
on Oct 12, 2012 at 15:42 UTC ( #998727=note: print w/replies, xml ) Need Help??

in reply to pattern matching

Hello perltux, and welcome to the Monastery!

How do I get the second pattern match to assign all matches as multiple elements to the array?

I think you need to capture the multiple matches first, then separate them using a loop:

my @chunks = $sysex_dump =~ /\xF0C.[~z]((?:..LM 0087[A-Z][A-Z].+?)+)\ +xF7/gs; my @parts; for (@chunks) { push @parts, $1 while /(..LM 0087[A-Z][A-Z].+?)/gs; }

This code is untested (you provided no sample data), but it should give you a workable approach. Note that in the first regex, the second + quantifier needs to be within the capturing parentheses, so I have added non-capturing parentheses (?:) to define this quantifier’s scope.

Update: I see you have added a “self contained code example” to your original post. From this, it appears that the data to be matched contains literal . characters (periods, full stops.) But your regex matches these with:

(..LM 0087[A-Z][A-Z].+?) # ^^ <= here they are

which matches any two characters. To match periods only, backslash them in the regex:

/\xF0C.[~z](\.\.LM 0087[A-Z][A-Z].+?)+\xF7/s

Hope that helps,

Athanasius <°(((><contra mundum

Replies are listed 'Best First'.
Re^2: pattern matching
by perltux (Scribe) on Oct 12, 2012 at 16:14 UTC
    Thanks,looks like this works but it's very contrived, surely there must be an easier way to do it all in on go?
Re^2: pattern matching
by perltux (Scribe) on Oct 12, 2012 at 18:39 UTC
    I have tried this now but unfortunately it still doesn't work, the second match in the line starting with 'push' matches too few bytes, it needs to match all bytes until the next occurence of (..LM 0087A-ZA-Z.+?). If I remove the '?' at the end then it matches all occurrences at once. How do I get this match right to match all bytes until the next occurence of (..LM 0087A-ZA-Z.+?) ?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://998727]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (7)
As of 2018-02-20 14:20 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (271 votes). Check out past polls.