Regexp for special string
I need a regexp 开发者_Go百科which matches to the sting which
- starts with $
- ends with $
- contains some alphabetical characters (not defined how many)
e.g.
$ABC$ # ok
$ABCDEF$ # ok
$ABC # not ok
AC$ # not ok
This is the regex you want:
/^\$[a-z]+\$$/i
Components explained:
^
- Start matches only from the beginning of the input\$
- Match a literal$
(the\
is used for escaping special chars)[a-z]
- Match any of the letters (combined with thei
modifier at the end matches both lowercase and uppercase)+
- match items from the preceding character class 1 or more times\$
- See 2$
- End matches only at the end of the input/
- Is the standard delimited for regex, but it may vary in some languagesi
- Placed at the end is the Case Insensitive modifier
Good reading: http://www.regular-expressions.info/
^\$[A-Za-z]*\$$
This will match 0 or more characters in between dollar signs. If you want to match one or more, use ^\$[A-Za-z]+\$$
.
This could probably be written with fewer characters, but I have no indication of what regex engine you're using, so I can't use any specific features.
Something like
^\$[a-zA-Z]+\$$
should work. Syntax depends on where do you use it
精彩评论