Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/21.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
Python 在另一个字符串中使用部分正则表达式和部分非正则表达式搜索文本进行搜索_Python_Regex_Python 3.x_Search - Fatal编程技术网

Python 在另一个字符串中使用部分正则表达式和部分非正则表达式搜索文本进行搜索

Python 在另一个字符串中使用部分正则表达式和部分非正则表达式搜索文本进行搜索,python,regex,python-3.x,search,Python,Regex,Python 3.x,Search,我有两个文件: efile = c:\myexternal.txt cfile = c:\mycurrent.txt myexternal.txt: Paris London Amsterdam New York mycurrent.txt(但可以是任何文本): 我要做的是对externalfile(原始文本)中的每一行在当前文件中进行搜索,但使用正则表达式边界: p、 e.: 我想在currentfile中查找externalfile中的所有城市,但不查找前面有“is”的城市,并且

我有两个文件:

efile = c:\myexternal.txt    
cfile = c:\mycurrent.txt
myexternal.txt:

Paris
London
Amsterdam
New York
mycurrent.txt(但可以是任何文本):

我要做的是对externalfile(原始文本)中的每一行在当前文件中进行搜索,但使用正则表达式边界:

p、 e.:
我想在currentfile中查找externalfile中的所有城市,但不查找前面有“is”的城市,并且所有城市必须在cityname之后有空格,或者必须位于行的末尾:

boundO = "(?<!is\s)"
boundC = "(?=\s|$)"
#boundO + line in externalfile + boundC
#(regex rawtext regex)

#put every line of external file (c:\myexternal.txt) in list:
externalfile=[]
with open(efile, 'r+', encoding="utf8") as file:
  for line in file:
      if line.strip():                 #if line != empty
          line=line.rstrip("\n")       #remove linebreaks
          line=boundO + line + boundC  #add regex bounderies
          externalfile.append(line)

results = []
#check every line in c:\mycurrent.txt
with open(cfile, 'r+', encoding="utf8") as file:
  for line in file:
      if any(ext in line for ext in externalfile):
          results.append(line)

boundO=“(?您需要
re.search
。使用

with open("check.pl", 'r+') as file:
    for line in file:
        if any(re.search(ext, line) for ext in externalfile): # <---here
            print(line)
            results.append(line)
编辑

我不确定,但是,检查一下这个

boundO = "(?<!is\s)\\b"
boundC = "(?=\s|$)"
#boundO + line in externalfile + boundC
#(regex rawtext regex)

#put every line of external file (c:\myexternal.txt) in list:
externalfile=[]
with open("check", 'r+') as file:
  for line in file:
      if line.strip():                 #if line != empty
          line=line.rstrip("\n")       #remove linebreaks
          #line=boundO + line + boundC  #add regex bounderies
          externalfile.append(line)

results = []
print(externalfile)
#check every line in c:\mycurrent.txt
with open("check.pl", 'r+') as file:
    for line in file:
        if any(re.search(boundO + ext + boundC, line) for ext in externalfile):
            print(line)
            results.append(line)

boundO=“(?您需要
re.search
。使用

with open("check.pl", 'r+') as file:
    for line in file:
        if any(re.search(ext, line) for ext in externalfile): # <---here
            print(line)
            results.append(line)
编辑

我不确定,但是,检查一下这个

boundO = "(?<!is\s)\\b"
boundC = "(?=\s|$)"
#boundO + line in externalfile + boundC
#(regex rawtext regex)

#put every line of external file (c:\myexternal.txt) in list:
externalfile=[]
with open("check", 'r+') as file:
  for line in file:
      if line.strip():                 #if line != empty
          line=line.rstrip("\n")       #remove linebreaks
          #line=boundO + line + boundC  #add regex bounderies
          externalfile.append(line)

results = []
print(externalfile)
#check every line in c:\mycurrent.txt
with open("check.pl", 'r+') as file:
    for line in file:
        if any(re.search(boundO + ext + boundC, line) for ext in externalfile):
            print(line)
            results.append(line)

boundO=“(?regex需要编译才能使用

ext in line 
仅在第行中找到字符串ext时进行测试

您应该使用以下内容:

import re
regc=re.compile(ext)
regc.search(line)

在使用之前需要编译regex

ext in line 
仅在第行中找到字符串ext时进行测试

您应该使用以下内容:

import re
regc=re.compile(ext)
regc.search(line)
您必须在
运算符中使用而不是

if any(re.search(ext, line) for ext in externalfile):
并且,为了防止文件中的文本被解释为regex,请使用:

您必须在
运算符中使用而不是

if any(re.search(ext, line) for ext in externalfile):
并且,为了防止文件中的文本被解释为regex,请使用:



重要的是,外部文件中的行不被视为正则表达式,只被视为边界。(顺便说一句,曼彻斯特也是一个匹配)@Reman当外部文件不在第一个文件中时,
Manchester
怎么可能是匹配的?@Reman你说的外部文件不被视为regex是什么意思,只有边界。你说得对,Manchester不是匹配的。-外部文件中的文本必须是原始文本。..p.e.如果外部文件中有“\”,那么这个字符必须被视为“\”在正则表达式中不是反斜杠。是的,城市名称只是一个例子。外部文件可以包含所有类型的文本。重要的是,外部文件中的行不被视为正则表达式,而只被视为边界。(顺便说一句,曼彻斯特也是一个匹配)@Reman当外部文件不在第一个文件中时,
Manchester
怎么可能是匹配的?@Reman你说的外部文件不被视为regex是什么意思,只有边界。你说得对,Manchester不是匹配的。-外部文件中的文本必须是原始文本。。p.e.如果外部文件中有“\”,则此字符必须被视为'\“并且在正则表达式中不是反斜杠。是的,城市名称只是一个例子。外部文件可以包含所有类型的文本。边界不被视为正则表达式是什么意思?@rock321987,外部文件中的文本必须是原始文本。。p.e.如果外部文件中有“\”,则此字符必须被视为”\“在regex中不作为反斜杠,你说的边界不被视为regex是什么意思?@rock321987,外部文件中的文本必须是原始文本。.p.e.如果外部文件中有“\”,则此字符必须被视为”\"在RegExt中不是反斜杠这不是您需要的全部代码,而是您代码中缺少的部分。在开始时导入re,然后在代码中定义ext的位置插入re.compile和regc.search。出于性能原因,如果您的文件很大,在循环使用mycurrent.txt l之前,您应该先编译并存储每个正则表达式ines。这不是您需要的全部代码,只是代码中缺少的部分。在开始时导入re,然后在代码中定义ext的位置插入re.compile和regc.search。出于性能原因,如果您的文件很大,在遍历mycurrent.txt行之前,您应该先编译并存储每个正则表达式。这确实有效。regex被视为正则表达式,文本被视为文本。非常感谢!但速度非常慢。如果我不使用re.search,但使用我的
如果有的话(ext-in-line表示externalfile中的ext)
需要2秒。如果我使用你的方法需要4分钟以上。[外部文件有8000行,currentfile有900行]。你知道我该怎么做吗?@Reman为了加快速度,你可以将正则表达式和字符串操作结合起来,比如:
如果有的话(行中的ext和外部文件中的ext的re.search(boundO+re.escape(ext)+boundC,line):
。你确定吗?我的文件中没有匹配项。@Reman你必须删除
行=boundO+re.escape(行)+boundC
line让它起作用。这确实起作用。正则表达式被视为正则表达式,文本被视为文本。非常感谢!但它非常慢。如果我不使用re.search,但使用我的
如果有(外部文件中的ext在line中表示ext在externalfile中)
则需要2秒。如果我使用你的方法,则需要4分钟以上。[外部文件有8000行,currentfile有900行]。你知道我该如何加快速度吗?@Reman要加快速度,你可以将正则表达式和字符串操作结合起来,如有:
(行中的ext和重新搜索(boundO+re.escape(ext)+boundC,line)以查找外部文件中的ext):
。你确定吗?我的文件中没有任何匹配项。@Reman你必须删除
行=boundO+re.escape(line)+boundC才能工作。