Regex : How many times to match?

Place these modifiers after a character:

  • + matches 1+ times
  • * matches 0+ times
  • {3} after a charater matches it 3 times
  • {2,3} after a charater matches it 2 to 3 times
Greedy and Not Greedy


$x = "

one

two

";

#.* is greedy, so this matches from the first

to the last



$x =~ m#

.*

#x

#.*? isn't greedy, so this matches

one

only

$x =~ m#

.*?

#x

No comments:

Post a Comment