如何在文化獨立的方式下匹配單詞但不匹配字母?正則表達式匹配
\w
匹配單詞或數字,但我想忽略數字。所以,「111或者這個」與\w\s
將不起作用。
我想只得到「或這個」?我想{^[A-Za-z]+$}
不是解決方案,因爲德語字母表有一些額外的字母。
如何在文化獨立的方式下匹配單詞但不匹配字母?正則表達式匹配
\w
匹配單詞或數字,但我想忽略數字。所以,「111或者這個」與\w\s
將不起作用。
我想只得到「或這個」?我想{^[A-Za-z]+$}
不是解決方案,因爲德語字母表有一些額外的字母。
我認爲正則表達式應該是[^ \ d \ s] +。即不是數字或空格字符。
這應該匹配單詞工作:
\b[^\d\s]+\b
擊穿:
\b - word boundary
[ - start of character class
^ - negation within character class
\d - numerals
\s - whitespace
] - end of character class
+ - repeat previous character one or more times
\b - word boundary
這將匹配任何被明確排除數字和空格(所以「字」如「字邊界劃定aa?aa!aa「將被匹配)。
另外,如果您想排除這些,以及,你可以使用:
\b[\p{L}\p{M}]+\b
擊穿:
\b - word boundary
[ - start of character class
\p{L} - single code point in the category "letter"
\p{M} - code point that is a combining mark (such as diacritics)
] - end of character class
+ - repeat previous character one or more times
\b - word boundary
我會建議使用此:
foundMatch = Regex.IsMatch(SubjectString, @"\b[\p{L}\p{M}]+\b");
哪樣只匹配所有的unicode 字母。
雖然@ Oded的答案也可以工作,但它也與此匹配:p+ü+üü++üüü++ü
這不完全是一個單詞。
說明:
"
\b # Assert position at a word boundary
[\p{L}\p{M}] # Match a single character present in the list below
# A character with the Unicode property 「letter」 (any kind of letter from any language)
# A character with the Unicode property 「mark」 (a character intended to be combined with another character (e.g. accents, umlauts, enclosing boxes, etc.))
+ # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
\b # Assert position at a word boundary
"
使用此表達\b[\p{L}\p{M}]+\b
。 它使用不太知名的符號來匹配指定類別的Unicode字符(代碼點)。所以\p{L}
將匹配所有字母,並且\p{M}
將匹配所有組合標記。後者是必需的,因爲有時重音字符可能被編碼爲兩個代碼點(字母本身+組合標記),並且僅在這種情況下,\p{L}
將僅匹配其中的一個。
另請注意,這是匹配可能包含國際字符的單詞的一般表達式。例如,如果您需要一次匹配多個單詞或允許以數字結尾的單詞,則必須相應地修改此模式。
應該將「or this」視爲一個還是兩個? –
我想獲得模式「word1 word2」的匹配。請注意,「mark1是1」應該給我1匹配「mark1 is」。另外,「我的生日是11/08/2000」應該在「我的生日」和「生日是」中進行匹配(日期不應該匹配)。 – Nickolodeon