如何以独立于文化的方式匹配单词而不是字母?
\w
匹配单词或数字,但我想忽略数字。因此,带有 \w\s
的“111 或 this”将不起作用。
我只想得到“或这个”?我猜 {^[A-Za-z]+$}
不是解决方案,因为说德语字母表有一些额外的字母。
最佳答案
这应该适用于匹配词:
\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
关于.net - 正则表达式匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8288208/