Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Python3中使用正则表达式查找字符串?_Python_Regex - Fatal编程技术网

如何在Python3中使用正则表达式查找字符串?

如何在Python3中使用正则表达式查找字符串?,python,regex,Python,Regex,如何在Python3中使用正则表达式查找字符串 textfile.txt 21/02/2018 23/02/2018 yes/2s20/2620 A/RB2 417 A/FOüR COT Python代码 import re with open('textfile.txt','r') as f: input_file = f.readlines() b_list = [] for i in input_file: s = re.findall(r'^(?=.*/F)(?:

如何在Python3中使用正则表达式查找字符串

textfile.txt

21/02/2018
23/02/2018
yes/2s20/2620 A/RB2
417 A/FOüR COT
Python代码

import re
with open('textfile.txt','r') as f: 
     input_file = f.readlines()
b_list = []
for i in input_file:
     s = re.findall(r'^(?=.*/F)(?:[^/\n]*/){1,}[^/\n]*$|^(?=.*A/RB2$)(?:[^/\n]*/){3,}[^/\n]*$',i)
     if len(s) > 0:
        print(s)
        b_list.append(s)
print(b_list,"***********")
预期产出:

yes/2s20/2620 A/RB2
417 A/FOüR COT
全部清理完毕:

import re

b_list = []
match_string = re.compile(r'^(?=.*/F)(?:[^/\n]*/){1,}[^/\n]*$|^(?=.*A/RB2$)(?:[^/\n]*/){3,}[^/\n]*$')
with open('textfile.txt') as f:
    for i in f:
        match = match_string.match(i)
        if match:
            print(match.group(0))
            b_list.append(match.group(0)) #  Unsure what you need in b_list, this will only add the found string
原始答复:

尝试将for循环放在with语句下,并消除对readlines的需要

import re
with open('textfile.txt','r') as f:
    b_list = []
    for i in f:
         s = re.match(r'^(?=.*/F)(?:[^/\n]*/){1,}[^/\n]*$|^(?=.*A/RB2$)(?:[^/\n]*/){3,}[^/\n]*$',i)
         if s:
            print(s.group(0))
            b_list.append(s)
还可以使用
findall
,只是想弄清楚每行只匹配一个项目。使用原始代码:

     s = re.findall(r'^(?=.*/F)(?:[^/\n]*/){1,}[^/\n]*$|^(?=.*A/RB2$)(?:[^/\n]*/){3,}[^/\n]*$',i)
     if len(s) > 0:
        print(s[0])
        b_list.append(s)

我的回答是在编辑之前对您的原始问题的回答,但我认为这已经足够相似了,您可能仍然可以使用它

import re
d = """
"21/02/2018","23/02/2018","yes/2s20/2620 A/RB2","417 A/FOüR COT"
"""

regexpr1=r'\d\d\/\d\d/\d\d\d\d\"\,\"\d\d\/\d\d\/\d\d\d\d\",\"(.*?)\"'

s = re.findall(regexpr1, d)

print("Results for regexpr1 are")
print(s)

regexpr2=r'\"\,\"(.*?)\"'

s = re.findall(regexpr2, d)

for x in s:
    regexpr=r'\d\d\/\d\d/\d\d\d\d'
    z=re.findall(regexpr, x)
    if(z):
        s.remove(x)

print("Results for regexpr2 are")
print(s)
输出

Results for regexpr1 are
['yes/2s20/2620 A/RB2']
Results for regexpr2 are
['417 A/FOüR COT']

你在搜索什么?regex条件是正确的,但当我尝试迭代文本文件中的行并检查条件时,它出错了,我不知道为什么会发生这种情况,但我没有读取文本文件中的行,而是直接输入_file=“yes/2s20/2620 A/RB2”,“417 A/FOüR COT”这样它运行正确