我是正则表达的新手。我需要在正则表达式中验证字符串 -
Capital letters A-Z ' - and space
我是这个正则表达式概念的新手 -
and tried [A-Z,',-]* -> Any character in this class [A-Z,',/]. any number of repeatitions.
我试图验证并且我不是很自信因为我没有指定这个正则表达式甚至可以验证空格。 如果有人能提出建议或提供一些信息,我感激不尽
答案 0 :(得分:1)
你没有在[]中使用逗号分隔字符,所以你应该使用[A-Z'\ - ] * 你需要使用\ - 因为' - '在[]中有特殊含义。
答案 1 :(得分:1)
字符类中不需要逗号。所以下面应该适合你:
[A-Z' -]+
这意味着:
A-Z - Capital letters from A-Z (Range)
' - Single Quote
" " - Space (double quotes only to show you space)
- - Hyphen (must appear as 1st or last in character class in order to avoid escaping
[A-Z' -]+ - Match 1 or more of the above allowed characters
答案 2 :(得分:0)
你需要逃避特殊的字符:'和 -
[A-Z\-\']*
应该有用。
答案 3 :(得分:0)
对于大写字母A-Z' - 和空格
你可以使用[A-Z\s\'\-]
\s
用于空格,\'
用于'
,\-
用于-
,不需要逗号