Quantifiers

It allows us to specify how many characters we do need to search, let’s dive right in

? Optional

It allows to find a text that contains or not a character

For the demonstration we are going to use a file named file.txt with this content:

Java SE it's nice
I like JEE just a little bit
I prefer to avoid J2EE and use plain JavaSE
JavaFX is growing a lot

Finding lines with JEE or J2EE

$ grep -E "J2?EE" file.txt
I like JEE just a little bit
I prefer to avoid J2EE and use plain SEJava

* Exact Coincidences

Finding lines with Java text (no matter where, beginning-middle-ending)

$ grep -E "*Java" file.txt
Java SE it's nice
I prefer to avoid J2EE and use plain SEJava
JavaFX is growing a lot

+ A Quantity Of Coincidences

Let’s replace the content by:

1
2
12
21
123
213

Finding two or more digits

$ grep -E "[0-9][0-9]+" file.txt
12
21
123
213

{} Quantifiers for IDN

Let’s validate an identification number, we are looking for this pattern:

3_DIGITS - 4_DIGITS - 2_DIGITS - 5_DIGITS

For this example we are use a file with this content:

12345-4567-453465-45646
123-4568-45-45698
45646-454-545-99999
321-6548-55-87549
01236-21-45646-00
012-2546-5-00008

The expression that we need is:

$ grep -E "[0-9]{3}-[0-9]{4}-[0-9]{2}-[0-9]{5}" file.txt
123-4568-45-45698
321-6548-55-87549

Let’s suppose that valid IDN is:

3or5_DIGITS-4_DIGITS-2orMORE_DIGITS-5DIGITS

The expression that we need in this case is:

$ grep -E "[0-9]{3,5}-[0-9]{4}-[0-9]{2,}-[0-9]{5}" file.txt
12345-4567-453465-45646
123-4568-45-45698
321-6548-55-87549

If you like this post please pay me with a click on the ads :)