Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.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 如何在忽略特殊字符的字符串后找到下一个9个字符?_Python_Regex_String - Fatal编程技术网

Python 如何在忽略特殊字符的字符串后找到下一个9个字符?

Python 如何在忽略特殊字符的字符串后找到下一个9个字符?,python,regex,string,Python,Regex,String,考虑以下字符串: str_test = 'This is a sample text NRC234456789 and this is another case AZN.1.Z.3.4.S.6.7.8.9 and this another case BSA 123 456 789 and final case SSR/789456123' 基本上,我需要在字符串中找到字符“NRC”、“AZN”、“BSA”和“SSR”所在的位置。然后,我需要提取接下来的9个数字..忽略任何非数字字符。所以它应该

考虑以下字符串:

str_test = 'This is a sample text NRC234456789 and this is another case AZN.1.Z.3.4.S.6.7.8.9 and this another case BSA 123 456 789 and final case SSR/789456123'
基本上,我需要在字符串中找到字符“NRC”、“AZN”、“BSA”和“SSR”所在的位置。然后,我需要提取接下来的9个数字..忽略任何非数字字符。所以它应该回来

在某些情况下,数字5被错误地写为S,数字2被错误地写为Z。我仍然需要识别这些情况,并将错误的S和Z分别改为5和2

result = ['NRC234456789', 'AZN123456789' , 'BSA123456789', 'SSR789456123']
我有我正在使用的代码

list_comb = ['NRC', 'AZN', 'BSA', 'SSR'] 
def findWholeWord(w): 
    return re.compile(r'\b({0})\b'.format(w), flags=re.IGNORECASE).search 
它返回找到字符串的位置..但我不确定下一步如何继续。 谢谢

使用此正则表达式识别模式。也许它可以帮助:

import re

str_test = 'This is a sample text NRC234456789 and this is another case AZN.1.2.3.4.5.6.7.8.9 and this another case BSA 123 456 789 and final case SSR/789456123'
regex = re.findall("([A-Z0-9.\s\/]{2,})",str_test)
result = []
如果非数字字符仅包含点、逗号和斜杠,则有一种解决方案:

for r in regex:
    result.append(r.replace(".","").replace(" ","").replace("/",""))
print (result)
或者,如果非数字字符可以是任何字符,则使用此循环:

for r in regex:
    result.append(re.sub("([^\d\w])","",r))
print (result)
输出:

['NRC234456789', 'AZN123456789', 'BSA123456789', 'SSR789456123']
['NRC234456789', 'AZN123456789', 'BSA123456789', 'SSR789456123']
更新

输出:

['NRC234456789', 'AZN123456789', 'BSA123456789', 'SSR789456123']
['NRC234456789', 'AZN123456789', 'BSA123456789', 'SSR789456123']
这是一种方法

例:

输出:

['NRC234456789', 'AZN123456789', 'BSA123456789', 'SSR789456123']
['NRC234456789', 'AZN123456789', 'BSA123456789', 'SSR789456123']
根据评论进行编辑


下面是一个简单的方法,首先使用这个正则表达式找到想要的文本

\b(?:NRC|AZN|BSA|SSR)(?:.?\d)+
使用提供的列表动态生成,然后从中删除任何非字母数字字符

编辑: 为了处理错误字符串,其中2被错误地写为Z,5被错误地写为S,您可以在字符串的第二部分替换它们,忽略最初的三个字符。此外,代码也进行了更新,所以它只选择后面的9位数字,而不是更多。这是我更新的Python代码

import re

s = 'This is a sample text NRC234456789 and this is another case AZN.1.Z.3.4.S.6.7.8.9 and this another case BSA 123 456 789 and BSA 123 456 789 123 456 final case SSR/789456123'

list_comb = ['NRC', 'AZN', 'BSA', 'SSR']
regex = r'\b(?:{})(?:.?[\dA-Z])+'.format('|'.join(list_comb))
print(regex)

for m in re.findall(regex, s):
 m = re.sub(r'[^a-zA-Z0-9]+', '', m)
 mat = re.search(r'^(.{3})(.{9})', m)
 if mat:
  s1 = mat.group(1)
  s2 = mat.group(2).replace('S','5').replace('Z','2')
  print(s1+s2)
打印校正值,其中S替换为5,Z替换为2


@SpghttCd应忽略“/”,并返回后面的9位数字。谢谢@完成了!好的观点。这在简单案例str_测试中失败='BSA 123 456 789 123 456',并将按要求打印所有数字,而不仅仅是9。这在简单案例s='BSA 123 456 789 123 456'中失败,并将按要求打印所有数字,而不仅仅是9。@ruohola:虽然OP的字符串中没有任何此类数据,甚至没有谈到它,但这很容易处理。我已经更新了代码,您可以验证。
NRC234456789
AZN123456789
BSA123456789
BSA123456789
SSR789456123