A quantifier in RegEx is a meta-character that has special meaning if it occurs at the beginning of an expression. If it occurs anywhere else, it’s treated as itself and simply matches characters—or not, depending on what type of quantifier it is.
The differences between greedy, reluctant, and possessive quantifiers in the regular expression are given in the table below:
Greedy | Reluctant | Possessive |
---|---|---|
1. It matches as much text as possible. | 1. It refers to how reluctant a pattern may be to consume some text when there are better matches available elsewhere. | 1. It just tries to match as little as possible. |
2. a+ matches one or more consecutive a character(s). |
2. a? matches an a character or nothing. |
2. a* matches zero or more consecutive a character(s). |
Let’s discuss all these approaches with coding examples in Go.
+
A greedy quantifier matches as much text as possible. In other words, a +
character attached to a word or series of characters tries to eat up as many of those letters as possible before stopping. We may use a greedy quantifier when searching for multiple keywords at once.
l+
matches one or more consecutive l
characters.
package mainimport ("regexp""fmt")func main() {var re = regexp.MustCompile(`l+`)var str = `hello lullaby`for i, match := range re.FindAllString(str, -1) {fmt.Println(match, "found at index", i)}}
regex
and fmt
packages.regexp
object that can be used to match against the text.for
loop that prints a slice of all the successive matches of the expression along with their indices.The word hello ismatched as “he ll
o” which is equal to a match because the number of consecutive characters counts as one, while the word lullaby is matched as "l
u ll
aby", which equals two matches.
?
The reluctant quantifier is indicated by a question mark at either end of its pattern space. The match that’s immediately before or after it isn’t included when it executes. Instead, it just tries to match as little as possible. The term reluctant doesn’t refer to unwillingness. Rather, it refers to how reluctant a pattern may be to consume some text when there are better matches available elsewhere.
a?
matches an a
character or nothing.
package mainimport ("fmt""regexp")func main() {var re = regexp.MustCompile(`ba?`)var str = `ba b a`for i, match := range re.FindAllString(str, -1) {fmt.Println(match, "found at index", i)}}
regex
and fmt
packages.regexp
object that can be used to match against the text.for
loop that prints a slice of all the successive matches of the expression along with their indices.The code snippet above returns two matches.
*
The possessive quantifier requires a matching character to be followed by a specified character. If it is followed by that specified character, then it doesn’t have to be repeated.
a*
matches zero or more consecutive a
character(s).
package mainimport ("regexp""fmt")func main() {var re = regexp.MustCompile(`ba*`)var str = `a ba baa aaa ba b`for i, match := range re.FindAllString(str, -1) {fmt.Println(match, "found at index", i)}}
regex
and fmt
packages.regexp
object that can be used to match against the text.for
loop that prints a slice of all the successive matches of the expression along with their indices.The code snippet above returns four matches.
In order to master regular expressions, RegEx, we must first understand how they work. Once we understand that a single RegEx may be used to search for multiple results, we can begin leveraging their power. However, specific quantifiers are better suited than others for certain situations. Our search result set is affected by the type of quantifier that’s used.