开发者

What does \'.- mean in a Regular Expression

I'm new to regular expression and I havi开发者_JAVA技巧ng 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?


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.


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.


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.


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.


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:

  • ....................
  • abaca af - .
  • AAfa- - ..
  • .z

And so on.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜