Reputation: 1115
I'm new to regular expression and I having trouble finding what "\'.-" means.
'/^[A-Z \'.-]{2,20}$/i'
So far from my research, I have found that the regular expression starts (^) and requires two to twenty ({2,20}) alphabetical (A-Z) characters. The expression is also case insensitive (/i).
Any hints about what "\'.-" means?
Upvotes: 1
Views: 11973
Reputation: 3175
Everything inside the square brackets is part of the character class, and will match a single character listed. In your example, the characters listed are the letters A through Z, a space, a single quote, a period, or a hyphen. (Note the hyphen must be listed last to avoid indicating a range, like A-Z.) Your full regular expression will match between 2 and 20 of the listed characters. The single quote is needed so the compiler knows you are not ending the string that defines the regular expression.
Some examples of things this will match:
And so on.
Upvotes: 0
Reputation: 33769
This regex is in a string. The backslash is there to escape the single quote so the string doesn't end early, in the middle of the regex. The dot and dash are just what they are, a period and a dash.
So, you were nearly right, except it's 2-20 characters that are letters, space, single quote, period, or dash.
Upvotes: 1
Reputation: 100041
It's quoting the quote.
The regular expression is ^[A-Z'.-]{2,20}$
.
In the programming language you are using, you write it as a quoted string:
'SOMETHING'
To get a single quote in there, it's been backslashed.
Upvotes: 0
Reputation: 61379
The character class is the entire expression [A-Z \'.-]
, meaning any of A
-Z
, space, single quote, period, or hyphen. The \
is needed to protect the single quote, since it's also being used as the string quote. This charclass must be repeated 2 to 20 times, and because of the leading ^
and trailing $
anchors that must be the entire content of the matching string.
Upvotes: 6
Reputation: 490283
It means to escape the single quote ('
) that delmits the regex (as to not prematurely end the string), and then a .
which means a literal .
and a -
which means a literal -
.
Inside of the character range, the .
is treated literally, and if the -
isn't part of a valid range, e.g. a-z
, then it is treated literally as well.
Your regex says Match the characters a-zA-Z '.-
between 2 and 20 times as the entire string, with an optional trailing \n
.
Upvotes: 2