Reputation: 913
What is the regexp "[a-z0-9]\+" suppose to mean? Or specifically what is the "\" suppose to mean here? Is it right the no string in the world that matches that regexp?
Upvotes: 3
Views: 476
Reputation: 48211
The slash ("\") here is used to escape the +
.
+
has a special meaning in regex: it means repeat the precceding item one or more times.
[a-z0-9]\+
means: Match one alphanumeric character and a plus sign ("+") [a-z0-9]+
means: Match one or more alphanumeric charactersYou can find more info on regex Quantifiers here.
You might also want to take a look at this Regular Expression Basic Syntax Reference.
Upvotes: 0
Reputation: 195249
it is hard to say. it depends on which RE (BRE? ERE? or PCRE) do you use.
if you use BRE, the \
gives +
special meaning.
if you use ERE/PCRE, \
takes special meaning of +
away.
an example would be clear (with grep, default using BRE):
kent$ echo "aaaaaa+"|grep -o 'a+'
a+
kent$ echo "aaaaaa+"|grep -o 'a\+'
aaaaaa
kent$ echo "aaaaaa+"|grep -oE 'a\+'
a+
kent$ echo "aaaaaa+"|grep -oE 'a+'
aaaaaa
Upvotes: 5
Reputation: 7610
A backslash (\
)escapes special characters to suppress their special meaning. So here the special meaning of +
is removed. It is considered as just a symbol. The usual meaning of +
is to repeat the preceding block 1 or more times.
So in this case [a-z0-9]\+
means a single lower case letter or a decimal digit followed by a +. a+
, b+
, 0+
, 1+
etc. are all valid expressions of this regular expression.
Upvotes: 0
Reputation: 149078
The \
in [a-z0-9]\+
means the literal plus character. It's needed because without the slash, the +
would be a one-or-more quantifier.
Compare:
[a-z0-9]\+
matches a Latin lower-case letter or number followed by a plus, e.g. y+
[a-z0-9]+
matches one or more Latin lower-case letter or numbers, e.g. xy4
Upvotes: 2
Reputation: 72930
It makes the "+" a literal + symbol. This would be matched by any character matching the first range followed by a + sign, such as c+
or 5+
.
Without the slash, the + would mean "one or more occurences", so strings like abc
, t63
.
Upvotes: 0