Meta-Characters
Meta-Characters
RegEx uses heavily meta-characters in order to easily find patterns inside a file
[] Brackets
It allows to define a range of characters for find them in a file.
For the demonstration we are going to use a file named file.txt with this content:
1
hello
2
goodbye
Finding alphabetic characters
$ grep "[a-z]" file.txt
hello
goodbye
Finding numbers
$ grep "[0-9]" file.txt
1
2
^ Negate
Finding no-alphabetic characters
$ grep "[^a-z]" file.txt
1
2
Finding no numbers
$ grep "[^0-9]" file.txt
hello
goodbye
Searching Text Starting and Ending With
For the sake of simplicity let’s change the content of the file by:
FirstName
LastName
Name1
1stName
^[ Starting With
Finding text starting with a number
$ grep "^[0-9]" file.txt
1stName
$ Ending With
Finding text ending in a number
$ grep "[0-9]$" file.txt
Name1
If you like this post please pay me with a click on the ads :)