繁体   English   中英

Python正则表达式可在文本文件中间找到特定单词

[英]Python regex to find specific word in the middle of a text file

我基本上有一个文本文件,我想搜索一个句子的中间词。 运行.py脚本时,我收到一条错误消息,指出found_state not defined

考虑以下文件:

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"

如果您的条件if re.search('(?<=#)\\w+',data):失败,则未声明found_state。 在for循环之前执行该操作。

因为您说您需要获取“中间词”,所以我理解您需要提取该词。 现在,如果有匹配项,您将获得整条线。

这是一段适合您的代码 (它会打印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)

看到这个演示 #(\\w+)模式将在#之后捕获字符char(1个或更多),并且仅返回这些捕获的子字符串,并将其全部extend添加到列表中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM