Regular Expressions — Grouping, Quantified Repetition and Alternation
In the previous articles, we have already covered basic Notations, some Meta Characters, Character Ranges and Repetitions etc.
In case, you haven’t gone through above, I highly recommend to cover them. You can find the same using below links.
Here, we will continue further topics like Grouping, Quantified repetition and Alternation etc.
Quantified Repetition
By using +, *, ? as meta character, we assure the preceding pattern will get repeated 0, 1 or infinite times. But what if I want to repeat some pattern for a specific number of times, say 3 or 4. This case is called quantified repetition.
For this we use this syntax :- {min, max}
- Here min is the minimum number of times the pattern must be repeated
- And max is the maximum number of times the pattern must be repeated, but its optional.
E.g.:- \d{2,4} will match numbers with min 2 and max 4 digits.
\d{2}, will match numbers with exactly 2 digits. (min and max both are equal to 2)
\d{2,}, here it will match numbers with 2 or more digits. (max in infinite)
So, for a real example, if we want to match some phone number with fixed pattern, like 123–124–6677 i.e. 3 digit followed by hyphen, followed by 3 digit followed by hyphen, and then 4 digits at the end.
We can write regex as :- /\d{3}-\d{3}-\d{4}/g
if our pattern is somewhat flexible and allows either 3 or 4 numbers in middle, then we will change the expression as:- /\d{3}-\d{3,4}-\d{4}/g
Grouping
Some times, we want to combine some pattern or character, to make a grouped expression. For e.g., if our text is like:- acd bcd abcd
And we want to match cd preceded by only ab combined, not either a or b individually. Then our regex will be :- /(ab)cd/g
Here ab will be treated a single pattern, and will be matched together everywhere.
This is called Grouping. To use this, we have to combine them between starting and closing brackets, i.e. ( and ).
For example, take this quote :- impossible itself says i am possible
Now, to match either possible or impossible, in this text, we can write regex like :- /(im)?possible/g
It will first combine im as one expression, match it either 0 or 1 time (using ?), and followed by possible.
Alternation
If we have multiple choices, to match the expression, then alternation comes handy. It is nothing but a simple OR operator i.e. |.
E.g. :- Good Morning Good Evening Good Afternoon Good Night
and we want to match either Good morning or Good night only, and not remaining.
we can write regex like :- /Good (Morning|Night)/g
For a more practical example, lets have a text like :- I like Apple Shake or Apple Juice but not Banana Shake or Fruity Juice.
Here, if we want to match only Apple things, then the regex will be like :- /Apple (Shake|Juice)/g
So, that is all for this article. We will next look about Lazy and Greedy behavior of regular expressions, in next article. Stay tuned.