問題:
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