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

Re: regex for multiple capture within boundary

by Sidhekin (Priest)
on Jul 14, 2006 at 21:58 UTC ( [id://561346]=note: print w/replies, xml ) Need Help??


in reply to regex for multiple capture within boundary

I want a single regex that grabs all the number characters between the spaces

Extreme regexing? I cannot resist such a question. This ought to do it:

@nums = $x =~ /(?:^\S*\s+|\G)[^\s\d]*(\d+)/g;

However, if it does not absolutely have to be a single regex, I suspect the maintainer will prefer one of ikegami's solutions. :-)

Update: I've been out-extremed! Not perhaps by the first of ikegami's latter solutions, which after all is straight-forward, and probably as maintainable as mine, but certainly by the second. It took me three attempts just to read it: My eyes glazed over twice!

... what's with the local *nums; though? Yet another update: Ah. Considerate of you. :-)

print "Just another Perl ${\(trickster and hacker)},"
The Sidhekin proves Sidhe did it!

Replies are listed 'Best First'.
Re^2: regex for multiple capture within boundary
by ikegami (Patriarch) on Jul 14, 2006 at 22:10 UTC
    I hope you're refering to one of these and not one of the following ;)
    local *nums; our @nums; $x =~ / \s (?: [^\s\d]* (\d+) (?{ push(@nums, $1) }) )* /x;

    or

    local *nums; our @nums; $x =~ / ^ (?> \S* \s ) \S*? (?<!\d) ( (?> \d+ ) ) (?{ push(@nums, $1) }) (?!) /x;
Re^2: regex for multiple capture within boundary
by ikegami (Patriarch) on Jul 14, 2006 at 23:58 UTC
    ... what's with the local *nums; though?

    I used package variables because regexps capture. Putting the regexp in a sub would only work once if I had used lexical variables instead of pacakge variables.

    our @nums; makes it so I can say @nums instead of @main::nums to refer to the package variable.

    local *nums works better than local @nums;. They both ensure that @main::nums has the same value when we're done as it did when we started. In other words, it makes sure we're not trampling over someone else's variables.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (1)
As of 2024-04-24 14:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found