Why can't I match the string
"1234567-1234567890"
with the given regular expression
\d{7}-\d{10}
with egrep
from the shell like this:
egrep \d{7}-\d{10} file
?
You regex doesn't work, because you're matching the beginning of the line, followed by one or more word-characters (doesn't matter if you use the non-capturing group (?:…) here or not), followed by any characters.
Pattern matching is used by the shell commands such as the ls command, whereas regular expressions are used to search for strings of text in a file by using commands, such as the grep command.
egrep
doesn't recognize \d
shorthand for digit character class, so you need to use e.g. [0-9]
.
Moreover, while it's not absolutely necessary in this case, it's good habit to quote the regex to prevent misinterpretation by the shell. Thus, something like this should work:
egrep '[0-9]{7}-[0-9]{10}' file
egrep
mini tutorialgrep
, ed
, sed
, egrep
, awk
, emacs
grep
vs egrep
vs other regex flavorsFor completeness:
Egrep does in fact have support for character classes. The classes are:
Example (note the double brackets):
egrep '[[:digit:]]{7}-[[:digit:]]{10}' file
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With