.net - 正则表达式匹配

标签 .net regex

如何以独立于文化的方式匹配单词而不是字母?

\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/

相关文章:

ios - 正则表达式,未知转义序列 '\s'

regex - Powershell正则表达式组正则表达式匹配但没有我的组。缺少了什么?

c# - 具有多个 IEnumerable<T> 接口(interface)的类 - 如何处理非泛型方法?

.net - 支持向后兼容性的类版本控制

c# - 公众号是做什么的?

javascript - Visual Studio 上的正则表达式错误?

.net - 为什么minus比mod慢?

c# - 获取所有字母字符的数组

javascript - 替换评论之间的内容

regex - 在 Windows 上的 perl 中启用颜色正则表达式调试