English

# Quantifiers in Regular Expression.

Common Quantifiers and their pattern matching?

## Explanation

Quantifiers are metacharacters that define how many times a particular match may or may not occur in a string.

The following are some of the quantifiers used in regular expression.
 Greedy No Greedy Allowed numbers for a match ? ?? 0 or 1 time + +? 1 or more times * *? 0 or more times {m} {m}? match exactly m times {m,} {m,}? match m or more times {m,n} {m,n}? match at least m times and at most n times

### Example:

String="xeagerate"
Greedy pattern ="/(.?)e/"
Result:x
Non-Greedy pattern="/(.??)e/"
Result:x

In the above example we use the character "?" which means the character or sequence preceding it should be matched "0 or 1" time, so both the greedy and non-greedy patterns match just first occurence of "x". The pattern check for the occurence of "e".

### Example:

String="ederat"
Greedy pattern ="/e(.+)e/)"
Result:derat
Non-Greedy pattern="/e(.+?)e/)"
Result:d

In the above example we use the quantifier "+" which means the character preceding should occur one or more times, in non greedy we print the character till the first occurence of "e" and the greedy pattern will print till the last occurence.

### Example:

String="exagrate"
Greedy pattern ="/e(.*)e/"
Result: xagrat
Non-Greedy pattern="/e(.*?)e/" Result: xagrat

In the above example we use the quantifier "*" which means the character preceding should occur zero or more times, the non-greedy, greedy pattern matches are same there is "zero" occurence of "e" in between the starting and ending "e".