How to match hyphens with Regular Expression?

后端 未结 6 1118
[愿得一人]
[愿得一人] 2020-11-21 23:36

How to rewrite the [a-zA-Z0-9!$* \\t\\r\\n] pattern to match hyphen along with the existing characters ?

6条回答
  •  灰色年华
    2020-11-21 23:52

    The hyphen is usually a normal character in regular expressions. Only if it’s in a character class and between two other characters does it take a special meaning.

    Thus:

    • [-] matches a hyphen.
    • [abc-] matches a, b, c or a hyphen.
    • [-abc] matches a, b, c or a hyphen.
    • [ab-d] matches a, b, c or d (only here the hyphen denotes a character range).

提交回复
热议问题