只是爲了將來的參考添加,如果你有谷歌Explain Regular Expression
幾個網站的工具來解析表達式,並將其分解成片斷,下面的解釋將顯示出來。
要以起始斜槓/
開始並結尾斜槓/
是您的分隔符。
甲分隔符可以是任何非字母數字,非反斜槓,非空白字符。
你的正則表達式:
( group and capture to \1:
[\s_;?!\/\(\)\[\]{}<>\r\n"] any character of: whitespace (\n, \r, \t, \f, and " "),
_ ; ? ! \/ \(\) \[ \] { } < > \r \n "
| OR
\. '.'
$ before an optional \n, and the end of the string
| OR
(?<= look behind to see if there is:
\D non-digits (all but 0-9)
) end of look-behind
[:,.\-] any character of: ':', ',', '.', '\-'
| OR
[:,.\-] any character of: ':', ',', '.', '\-'
(?= look ahead to see if there is:
\D non-digits (all but 0-9)
) end of look-ahead
) end of \1
從本質上講,你有你的\r\n
字符類中,並可以刪除的部分,因爲\s
將匹配那些本身。通常只有某些字符需要在字符類中轉義。
(?:[(){}\[\]<>\/!?;_"\s]|\.$|(?<=\D)([;.,-])|(?1)(?=\D))
見demo
希望這有助於!祝你好運。
最讓我失望的是所有逃脫的文字,而我現在剛剛完全理解了我所聽到的所謂「後視」(在這種情況下,由於「D」是大寫)和「向前看」。謝謝。 – TecBrat
@TecBrat不客氣,很高興我能提供幫助。 –
請注意,如果這些模式位於字符串的開頭或結尾,則不會刪除以下字符'[:,.-]'。爲了解決這個問題,你必須把變量改爲'(?<!\ d)'和'(?!\ d)'。另一個關於字符類的東西,你可以將第一個縮寫爲'[] [(){} <> \/\ s _;?!「]'並且你不需要轉義'-'(因爲它在('!,.-](?!\ d)' –