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
?
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
?
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
grep
, ed
, sed
, egrep
, awk
, emacs
grep
vs egrep
vs other regex flavors\d
, \s
, \w
, \b
, etc. Also \d
is not a prefix; it's a shorthand for the digit character class supported by many but not all flavors. –
Potion \d
shorthand, and everyone else followed later. –
Potion \d
with grep/egrep; you can use its expanded form [0-9]
which is practically the same thing, but slightly longer. In some flavors that supports Unicode, \d
is not the same as [0-9]
because it also includes some other Unicode digit characters. –
Potion [0-9]
, when I really mean [[:digit:]]
. Plus these character classes are supported almost everywhere, and are defined in POSIX. –
Tisbe For completeness:
Egrep does in fact have support for character classes. The classes are:
Example (note the double brackets):
egrep '[[:digit:]]{7}-[[:digit:]]{10}' file
[[:digit:]]
is worse than [[0-9]]
in every possible way. None of these are short hand, and they are harder to rememer than the default regex syntax. EG: [[:lower:]]
is harder to remember, read and write than [a-z]
–
Godber grep -E
i.e. egrep
supports both [:digit:]
and [0-9]
so where is the complaint? If you're comparing with \d
it's arguable. d could stand for anything, a bit like one letter variable names. Still seems \d has become more popular. I think grep character classes pre-date Perl \d
–
Immolation you can use \d
if you pass grep the "perl regex" option, ex:
grep -P "\d{9}"
grep -P
–
Dunfermline Use [0-9] instead of \d. egrep doesn't know \d.
grep
is \{7\}
. –
Potion try this one:
egrep '(\d{7}-\d{10})' file
© 2022 - 2024 — McMap. All rights reserved.