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
Python 如果字符串列表中存在字符串匹配项,则查找文档中某行中的数字_Python_Regex_String_String Matching - Fatal编程技术网

Python 如果字符串列表中存在字符串匹配项,则查找文档中某行中的数字

Python 如果字符串列表中存在字符串匹配项,则查找文档中某行中的数字,python,regex,string,string-matching,Python,Regex,String,String Matching,我有一个字符串列表。如果列表中的任何单词在文档中的一行内匹配, 我想得到匹配的单词和一个数字作为输出,它将出现在行中,主要是在匹配的单词之后。单词和数字主要由空格或空格分隔: 文件中的示例: Expedien: 1-21-212-16-26 我的名单: my_list = ['Reference', 'Ref.', 'tramite', 'Expedien'] 匹配字符串行内的数字可以用-分隔,也可以不用。 示例:1-21-22-45或RE9833 在这种情况下,如果在行内找到列表中的匹配

我有一个字符串列表。如果列表中的任何单词在文档中的一行内匹配, 我想得到匹配的单词和一个数字作为输出,它将出现在行中,主要是在匹配的单词之后。单词和数字主要由空格或空格分隔:

文件中的示例:

Expedien: 1-21-212-16-26 
我的名单:

my_list = ['Reference', 'Ref.', 'tramite', 'Expedien']
匹配字符串行内的数字可以用-分隔,也可以不用。 示例:1-21-22-45或RE9833

在这种情况下,如果在行内找到列表中的匹配单词,则RE9833应该完全不只是数字

如何用python为此编写正则表达式。

输入文件:

样本:

输出:


Regex演示:

到目前为止你做了什么?谢谢Allan。你能简单介绍一下你为什么使用国旗吗?不使用国旗也可以吗one@checkmate:国旗?哪一个?包含=错误。那个part@checkmate:我必须在此标志上使用if来分支for的行为,并在上一次迭代中找到列表中的元素时创建不同的行为。如果是这样的话,在当前的迭代中,我们知道我们将收到值,并且我们需要将值和键存储在dict中。当我在目录上运行时,我得到了很多垃圾数据。我只想把数字作为输出。你知道怎么解决吗?
$cat input_file
Expedien: 1-21-212-16-26 #other garbage
Reference RE9833 #tralala
abc
123
456
Ref.: UV1234
tramite  1234567
Ref.:
import re

my_list = ['Reference', 'Ref.', 'tramite', 'Expedien']

#open the file as input
with open('input_file','r') as infile:
  #create an empty dict to store the pairs
  #that we will extract from the file
  res = dict()
  #for each input line
  for line in infile:
    #the only place we will use regex in this code
    #we split the input strings in a list of strings using
    #as separator : if present followed by some spaces
    elems = re.split('(?::)?\s+', line)
    #we test that we have at least 2 elements 
    #if not we continue with the following line
    if len(elems) >= 2 :
      contains = False
      #tmp will store all the keys identfied
      tmp = ''
      #we go through all the strings present in this list of strings
      for elem in elems:
        #when we enter this if we have already found the key and we have the value
        #at this iteration
        if contains:
          #we store it in the dict
          #reset the check and leave this loop
          res.update({tmp : elem})
          contains = False
          break
        #we check if the elem is in my_list
        if elem in my_list:
          #if this is the case
          #we set contains to true and we save the key in tmp
          contains = True
          tmp = elem
  print(res)
python find_list.py
{'tramite': '1234567', 'Reference': 'RE9833', 'Expedien': '1-21-212-16-26', 'Ref.': ''}