Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Using qr// with a complicated regex

by elusion (Curate)
on Jul 12, 2002 at 20:28 UTC ( [id://181391]=perlquestion: print w/replies, xml ) Need Help??

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

My dearest brethren, I pray thee will help me on my quest for enlightenment.

I am, currently, building a large project, part of which involves a complicated parsing procedure, for which I am hoping to enlist the help of perl's regex engine. However, it is somewhat limited in the area I need help in. Take the following example regex: /(?:(\d)\w))+\d\w/ Not too complicated, but it will be, because if you know your regexes, you know that $1 will only contain the value of the last match. Meaning matching that regex against "1b2b3b4b5b" will result in $1 being equal to 4. There is a way to get to the other values, however. As japhy proposes in his book, the code:

my @vars; $string =~ /(?{local @1 = ();} (?:( (\d) (?{ local @1 = (@1, $1) }) \w )+ \d\w (?{ @vars = @1 /x
will match and stuff the vars in @vars, though in a somwhat roundabout way. My need goes even further, however, as I need to create these dynamically. I can, and have, done this, with one problem. In order to speed this up, I have tried to use qr//. I am doing this in an OO interface, so that it works well with the rest of my program. But qr// doesn't seem to like the above regex. It doesn't complain, but it somehow messes up other variables in my program. Objects that are in a different module altogether. I don't know how or why, but it is, even though I use warnings, strict, and what I think are good coding practices.

My question is, why does it do this? When I take the local command out of the regex, everything works, and my other variables aren't messed up. In order to get the speed and interface I want, I need to figure out how to do this. Thank you,

elusion : http://matt.diephouse.com

Replies are listed 'Best First'.
Re: Using qr// with a complicated regex
by Chmrr (Vicar) on Jul 12, 2002 at 21:29 UTC

    I'll say right off that I've no hints as to what's causing your variables to get muched up. But though you havn't said all that much about your real data, but it seems like you're trying to make this much more complicated than it needs to be. For example, using a regex with lookahead in list context would seem to be the way to go when solving your example problem:

    my @vars = $string =~ /\d(?=\w\d)/g;

    Is there any particular reason why you need to do it using the japhy's twistiness? Any reason why you can't just get all \ds and pop the last one off, either?

    perl -pe '"I lo*`+$^X$\"$]!$/"=~m%(.*)%s;$_=$1;y^`+*^e v^#$&V"+@( NO CARRIER'

      Well my example problem is just an example that represents the basic problem. I don't have any foreknowledge of the regexes that will be used, because they're all generated dynamically from user input that's supposed to be as general as possible. I'm sure that most of the time I'd be safe with reshaping the regexes like this:
      my @vars; / (?: (\d) (?{ push @{ $vars[1] }, $1 }) \w )+ \d\w/x;
      But I want things to work under all possible cases. That's basically where things stand: I don't have a concrete example where it's necessary, but I don't want to not work when that situation comes up. I'll either try to live with the speed I have, (de|re)structure everything, or roll my own finite state machine.

      elusion : http://matt.diephouse.com

Re: Using qr// with a complicated regex
by BrowserUk (Patriarch) on Jul 13, 2002 at 00:36 UTC

    Probably just a typo, but aren't there a couple of brackets missing from the last line of your final regex?


    Anyone know of an abbottoire going cheap?

      Yup. That's a typo. Sorry 'bout that, that's what I get for making up code instead of using cut-and-paste. ;-) It should be:
      my @vars; $string =~ /(?{local @1 = ();} (?:( (\d) (?{ local @1 = (@1, $1) }) \w )+ \d\w (?{ @vars = @1 })/x;

      elusion : http://matt.diephouse.com

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://181391]
Approved by dimmesdale
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (2)
As of 2024-04-24 16:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found