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

Re: RE: Quantifiers in regular expressions

by ninja-joe (Monk)
on May 19, 2004 at 15:59 UTC ( [id://354733]=note: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


in reply to RE: Quantifiers in regular expressions
in thread Quantifiers in regular expressions

Here's a simple modification to the example code that will show what the regex matched when you typed it in:
#!/usr/bin/perl while(<>) { chomp; # chomp so this next output is pretty. # newlines aren't discarded when you <> print "\"$1\" was matched out of \"$_\"" if m/(your_pattern)/; }
Be sure to include the parenthesis around the entire regex that way it will save what it matches in $1.

Do recall that while the regex operators are greedy by default you can suffix them with ? and they'll go to nongreedy. An example:
#!/usr/bin/perl while(<>) { chomp; print "\"$1\" was matched out of \"$_\"\n" if m/(\w{5}?)/; }
This will match "mywor" out of "myword"

Have fun regexing.

Replies are listed 'Best First'.
Re: Re: RE: Quantifiers in regular expressions
by Roy Johnson (Monsignor) on May 19, 2004 at 16:25 UTC
    Putting a ? on a quantifier that isn't variable-length is like having any color car you want, as long as it's black. There's no difference between a greedy and a non-greedy {5}.

    A greedy quantifier will take as many characters as it can, and then start backtracking until whatever follows it matches. A non-greedy one will take as few characters as it can, and then take more until whatever is supposed to follow it is found.


    The PerlMonk tr/// Advocate

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://354733]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.