使用正则表达式、Python匹配表达式
我有很多句子,不过我会创建一个函数,分别对每个句子进行操作。所以输入只是一个字符串。我的主要目标是提取介词后面的单词,如使用正则表达式、Python匹配表达式,python,regex,string,list,Python,Regex,String,List,我有很多句子,不过我会创建一个函数,分别对每个句子进行操作。所以输入只是一个字符串。我的主要目标是提取介词后面的单词,如“near blue meadows”我想提取blue meadows。 我所有的介词都在一个文本文件中。它工作正常,但我想使用的正则表达式有问题。这是我的密码: 进口稀土 with open("Input.txt") as f: words = "|".join(line.rstrip() for line in f) pattern = re.compile
“near blue meadows”
我想提取blue meadows
。我所有的介词都在一个文本文件中。它工作正常,但我想使用的正则表达式有问题。这是我的密码: 进口稀土
with open("Input.txt") as f:
words = "|".join(line.rstrip() for line in f)
pattern = re.compile('({})\s(\d+\w+|\w+)\s\w+'.format(words))
text3 = "003 canopy grace appt, classic royale garden, hennur main road, bangalore 43. near hennur police station"
print(pattern.search(text3).group())
这将返回:
AttributeError Traceback (most recent call last)
<ipython-input-83-be0cdffb436b> in <module>()
5 pattern = re.compile('({})\s(\d+\w+|\w+)\s\w+'.format(words))
6 text3 = ""
----> 7 print(pattern.search(text3).group())
AttributeError: 'NoneType' object has no attribute 'group
预期输出:
['near','nr','opp','opposite','behind','towards','above','off']
hennur police
该文件包含Python列表文本。用于分析文本
>>> import ast
>>> ast.literal_eval("['near','nr','opp','opposite','behind','towards','above','off']")
['near', 'nr', 'opp', 'opposite', 'behind', 'towards', 'above', 'off']
输出(第一行打印在循环的
中,第二行来自搜索(..)。组(1)
):
注意如果单词中有任何特殊字符在正则表达式中具有特殊含义,您需要检查每个单词。您需要检查单词
中的确切内容。这对我很有用(尽管您实际上应该在亨努尔警察局附近找到),因此,您确实需要仔细检查Input.txt
是否正确(每行一个单词)。Input.txt的格式为['near'、'off'、'contract'…]等等。。我已经编辑了我的问题。检查它。文件的内容是“[‘近’、‘nr’、‘opp’、‘对’、‘后’、‘对’、‘上’、‘关’””
还是[‘近’、‘nr’、‘opp’、‘对’、‘后’、‘对’、‘上’、‘关’]”
?(是否用引号括起来)输入文件没有引号。。名为words的变量有双引号
import ast
import re
with open("Input.txt") as f:
words = '|'.join(ast.literal_eval(f.read()))
pattern = re.compile('(?:{})\s(\d*\w+\s\w+)'.format(words))
text3 = "003 canopy grace appt, classic royale garden, hennur main road, bangalore 43. near hennur police station"
# If there could be multiple matches, use `findall` or `finditer`
# `findall` returns a list of list if there's capturing group instead of
# entire matched string.
for place in pattern.findall(text3):
print(place)
# If you want to get only the first match, use `search`.
# You need to use `group(1)` to get only group 1.
print pattern.search(text3).group(1)
hennur police
hennur police