开发者

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:

  1. ^ - Start matches only from the beginning of the input
  2. \$ - Match a literal $ (the \ is used for escaping special chars)
  3. [a-z] - Match any of the letters (combined with the i modifier at the end matches both lowercase and uppercase)
  4. + - match items from the preceding character class 1 or more times
  5. \$ - See 2
  6. $ - End matches only at the end of the input
  7. / - Is the standard delimited for regex, but it may vary in some languages
  8. i - 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

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜