正则表达式 - 连字符应该被转义吗? [复制] - Regex - Should hyphens be escaped? [duplicate]

问题:

This question already has answers here : 这个问题在这里已经有了答案
Closed 9 years ago . 9年前关闭。

Possible Duplicate:可能的重复:
How to match hyphens with Regular Expression? 如何将连字符与正则表达式匹配?

Hyphen is a special character in regex, for instance, to select a range, I could do something like:连字符是正则表达式中的一个特殊字符,例如,要选择一个范围,我可以执行以下操作:

[0-9A-F]

But outside of square brackets it's just a regular character right?但是在方括号之外它只是一个普通字符,对吗? I've tested this on a couple of online regex testers, and hyphens seem to function as a normal character outside of square brackets (or even inside of square brackets if it's not in-between two characters - eg [-g] seems to match - or g) whether it's escaped or not.我已经在几个在线正则表达式测试器上对此进行了测试,连字符似乎可以作为方括号外的普通字符(如果不在两个字符之间,甚至在方括号内 - 例如 [-g] 似乎匹配- 或 g) 它是否被转义。 I couldn't find the answer to this, but I'm wondering whether or not it is conventional to escape hyphens.我找不到这个问题的答案,但我想知道逃避连字符是否是惯例。

Thanks!谢谢!


解决方案:

参考一: https://en.stackoom.com/question/eEYU
参考二: https://stackoom.com/question/eEYU
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章