输入字符串
我曾经'findall'
找过只有字母和数字的单词(未指定要查找的单词数)。
我创建:
words = re.findall ("\ w * \ s", x) # x is the input string
如果我"asdf1234 cdef11dfe a = 1 b = 2"
分开输入这些句子,asdf1234, cdef11dfe, a =, 1, b =, 2
我只想挑出asdf1234
,cdef11dfe
正则表达式怎么写?
这个应该工作: (?<![\"=\w])(?:[^\W_]+)(?![\"=\w])
解释
(?:[^\W_])+
至少一次非单词字符或下划线以外的任何字符(非捕获组)
(?<![\"=\w])
前面没有"
或一个单词字符
(?![\"=\w])
后面没有"
或一个单词字符
示例代码 在线运行
import re
regex = r"(?<![\"=\w])(?:[^\W_]+)(?![\"=\w])"
test_str = "a01a b02 c03 e dfdfd abcdef=2 b=3 e=4 c=\"a b\" aaa=2f f=\"asdf 12af\""
matches = re.finditer(regex, test_str)
for matchNum, match in enumerate(matches):
print (match.group())
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句