What is a regular expression?
Regular Expression or regex, as assumed by many, is not a tool or software. Regular expression is a sequence of characters that define a search pattern. This was given back in 1950 by mathematician Stephen Cole Kleene who then formalized the description of a regular language. you can read more details on Wikipedia here https://en.wikipedia.org/wiki/Regular_expression
What is a pattern?
A pattern is a sequence of text characters which posses some common traits in the given strings. lets take an example below
Case 1)
- ct cat caat caaat caaaa...t
- In the above the pattern is first character is 'c' and the last character should be 't', in between there can either be multiple occurrence of 'a' or not at all.
- This can be represented in terms of regular expression as:
- ca*t
- Where '*' is a meta-character saying that the previous character can occur either zero times or n number of times
Case 2)
- cat cat caat caaat caaaa...t
- In the above the pattern is first character is 'c' and the last character should be 't', in between there can be the character 'a' at least once or N number of times.
- This can be represented in terms of regular expression as
- ca+t
- Where '+' is a meta-character saying that the previous character has to occur at least one times or n number of times
Case 3)
- ct or cat
- In the above the pattern is first character is 'c' and the last character should be 't', in between there can be the character 'a' at least once or not at all.
- This can be represented in terms of regular expression as
- ca?t
- Where '?' is a meta-character saying that the previous character has to may or may not occur i.e. 0 or 1 occurrences.
The above examples are just the tip of the ice-berg. Of course the regex can be made more complex with other meta-characters and parenthesis for matching complicated patters like dates, time, IP addressees, email id, domain name and lot more.
Some one has even written a regex for checking prime numbers also.
How do we use this in GNU/Linux?
It is not exactly correct to say how we use it in Linux. First as we have understood regex is a way with which you can match patterns in text file or a given input in which is in text format.
So first question yourself where do you want to search this pattern? Then look for a tool that supports regex.
In a GNU/Linux system commands like grep, sed, awk, vi and others support regex. Programming languages, PERL, Python, php have functions to support/user regex even shell scripting supports regex upto a certain extent.
On the GUI side many text editors, file search tools etc support the use of regex. Even there support in LibreOffice search and replace option.
So there is no single point of entry as regex is not an application or tool, but will be supported by different applications.
So basically there is no specific single tool that is the only thing there for using regular expression. Learn how to create regular expressions and then you should be able to use that efficiently as per your requirement with a suitable tool.
Add new comment