Python 正则表达式排除下划线

标签 python regex

我需要找到 UNICODE 中的所有双字符符号,下划线除外。当前的解决方案是:

pattern = re.compile(ur'(?:\s*)(\w{2})(?:\s*)', re.UNICODE | re.MULTILINE | re.DOTALL)
print pattern.findall('a b c ab cd vs sd a a_ _r')
['ab', 'cd', 'vs', 'sd', 'a_', '_r']

我需要从正则表达式中排除下划线 _,因此找不到 a__r。问题是,我的角色可以使用任何语言。所以我不能像这样使用正则表达式:[^a-zA-Z]。例如,在俄语中:

print pattern.findall(u'ф_')

最佳答案

排除任何非单词字符 AND _

[^\W_]

代替

\w

关于Python 正则表达式排除下划线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12589791/

相关文章:

python - 3D 绘图 : why are coordinate arguments input as 2D arrays?

python - 为什么 list.remove() 不起作用 "properly"?

regex - 使用 AWK [或 sed、grep 等] 删除特定行

java - 此 Java 正则表达式将返回单个结果还是多个

python - Python正则表达式中的groups()方法

regex - 文件系统正则表达式搜索工具

python - 裁剪 .pdf 文件的页面

python - 查找 plist 中是否包含具有特定字符串的键

python - 数据是包含标题和小节的列,如何拆分它?

regex - 在大文本中找到某种模式的有效方法是什么?