正則表達式 - 連字符應該被轉義嗎? [複製] - 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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章