Pattern matching digits does not work in egrep?
Why can't I match the string
"1234567-1234567890"
with the given regular ex开发者_Python百科pression
\d{7}-\d{10}
with egrep
from the shell like this:
egrep \d{7}-\d{10} file
?
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
See also
egrep
mini tutorial
References
- regular-expressions.info/Flavor comparison
- Flavor note for GNU
grep
,ed
,sed
,egrep
,awk
,emacs
- Lists the differences between
grep
vsegrep
vs other regex flavors
- Lists the differences between
- Flavor note for GNU
For completeness:
Egrep does in fact have support for character classes. The classes are:
- [:alnum:]
- [:alpha:]
- [:cntrl:]
- [:digit:]
- [:graph:]
- [:lower:]
- [:print:]
- [:punct:]
- [:space:]
- [:upper:]
- [:xdigit:]
Example (note the double brackets):
egrep '[[:digit:]]{7}-[[:digit:]]{10}' file
you can use \d
if you pass grep the "perl regex" option, ex:
grep -P "\d{9}"
Use [0-9] instead of \d. egrep doesn't know \d.
try this one:
egrep '(\d{7}-\d{10})' file
精彩评论