#grep #tips
## Basic Regular Expressions (BRE)
Basic Regular Expressions are the default regular expression syntax used by
Some key points about BRE:
- Metacharacters like
- The
- Alternation is done using
- Repetition operators like
For example, to match lines containing either "cat" or "dog" using BRE:
And to match lines with 3 to 6 digits:
The backslashes are necessary to use the grouping and repetition metacharacters in BRE syntax.
So in summary, BRE provides a more limited set of regex features compared to ERE, but is still very powerful for common text matching tasks. The main difference is the need to escape certain metacharacters in BRE.
One thing I would to mention here is that most metacharacters in Basic Regular Expressions (BRE) need to be escaped, but the
### Historical Context
In the early days of Unix, the
### Why Not Escape
Escaping every special character would make BRE more verbose and less readable, especially for simple patterns. By treating
### Example
For instance, to match any number of characters before or after a specific string, you can use:
This will match any line containing "pattern" followed by zero or more characters.
### Summary
While most metacharacters in BRE require escaping, the
## Basic Regular Expressions (BRE)
Basic Regular Expressions are the default regular expression syntax used by
grep
when no special options are provided. BRE uses a more limited set of metacharacters compared to Extended Regular Expressions (ERE).Some key points about BRE:
- Metacharacters like
+
, ?
, |
, (
, )
are treated as literal characters and need to be escaped with a backslash (\
) to use their special meaning.- The
$$
and $$
constructs are used for grouping in BRE, instead of just (
and )
as in ERE.- Alternation is done using
\|
instead of just |
.- Repetition operators like
*
and \{n,m\}
are used to match zero or more and ranges of repetitions respectively.For example, to match lines containing either "cat" or "dog" using BRE:
grep 'cat\|dog' filename
And to match lines with 3 to 6 digits:
grep '\([0-9]\{3,6\}\)' filename
The backslashes are necessary to use the grouping and repetition metacharacters in BRE syntax.
So in summary, BRE provides a more limited set of regex features compared to ERE, but is still very powerful for common text matching tasks. The main difference is the need to escape certain metacharacters in BRE.
One thing I would to mention here is that most metacharacters in Basic Regular Expressions (BRE) need to be escaped, but the
*
character does not. This might seem inconsistent, but it's due to historical reasons and the way BRE was designed.### Historical Context
In the early days of Unix, the
grep
command was developed, and it used BRE as its default syntax. At that time, the *
character was already a special character in many programming languages and shell scripts, often used for wildcard matching. To avoid conflicts and make BRE more intuitive for users familiar with shell globbing, the designers of grep
decided to treat the *
character as a special character without needing an escape.### Why Not Escape
*
?Escaping every special character would make BRE more verbose and less readable, especially for simple patterns. By treating
*
as a special character without an escape, it simplifies common tasks like matching any number of characters (including zero) before or after another character.### Example
For instance, to match any number of characters before or after a specific string, you can use:
grep 'pattern*' filename
This will match any line containing "pattern" followed by zero or more characters.
### Summary
While most metacharacters in BRE require escaping, the
*
character is an exception due to its historical significance and the desire to keep BRE syntax simple and intuitive. This makes it easier for users to learn and use basic regular expressions without needing to worry about escaping every special character.