Python正则表达式在文本文件的中间找到特定单词
问题描述:
我基本上有一个文本文件,我想搜索一个句子的中间单词。当我运行.py
脚本时,出现found_state not defined
错误。Python正则表达式在文本文件的中间找到特定单词
考虑这个文件:
file.conf
hostname(config)#aaa new-model
fdfsfd b
kthik
pooooo
shh
我的python脚本的样子:
import re;
import time;
with open('file.conf') as f:
content = f.readlines()
name=''
for data in content:
if re.search('(?<=#)\w+',data):
found_state=1
name=data
break
if found_state==1:
print name + "is Found"
else:
print "NF"
答
既然你说你需要得到“中间词”我知道你需要提取那个词。现在,如果有一场比赛,你会得到整条线。
这里是a piece of code应该为你工作(它打印aaa is Found
):
import re;
content = ["hostname(config)#aaa new-model", "fdfsfd b", "kthik", "pooooo", "shh"] # <= TEST DATA
name=''
found_state = 0 # Declare found_state
for data in content:
m = re.search(r'#(\w+)',data) # Use a raw string literal and a capturing group
if m: # Check if there was a match and if yes
found_state=1 # - increment found_state
name=m.group(1) # - get the word after #
break
if found_state==1:
print name + " is Found"
else:
print "NF"
不过,也许,你会想降低您的代码
res = []
for data in content:
res.extend(re.findall(r'#(\w+)', data))
print(res)
见this demo。 #(\w+)
模式将在#
之后捕获字符(1或更多),并且将仅返回这些捕获的子字符串,并且extend
将把它们全部添加到列表中。
+1
谢谢你的作品! – GoluBoss
因此,事先声明'found_state'。你在哪里使用脚本?除了首先声明变量之外,还可能需要在'if'块中指定'global found_state',然后将其分配给'1'。 –
好的,也许你只需要'r'#(\ w +)''然后'.group(1)'?见https://ideone.com/HdPCEt –
改进格式有点 – AlBlue