2024-04-25 05:08:57 发布
网友
我有这个密码
x = "('A', 'NN'), ('B', 'AP'), ('C', 'RB'), ('D', 'NNP')" m = re.findall(r"\('(.*?)', 'NN[.]?'\)", x) print(m)
我想要的是提取像bcd这样的文本,只要它对应的元素是NN或者NNP,NND之类的NN的任何扩展。你知道吗
所以输出应该是A和D
但是电流输出是['A']
['A']
改用这个正则表达式解决方案,使用findall和匹配的组:
findall
re.findall(r"\('([^']+?)', 'NN.?'\)", x)
试试下面代码:-你知道吗
import re x = "('A', 'NN'), ('B', 'AP'), ('C', 'RB'), ('D', 'NNP')" extensions = ['NN','NNP','NND'] for a in re.findall("\((.*?)\)",x): test = re.search(r"^\'(\w)\',\s\'(\w+?)\'$",a) if test.group(2) in extensions: print test.group(1)
试试这个,用findall代替search
re.findall(r"\('([^']+)', 'NN[^']?'\)", x)
改用这个正则表达式解决方案,使用
findall
和匹配的组:试试下面代码:-你知道吗
试试这个,用findall代替search
相关问题 更多 >
编程相关推荐