[英]How to find next 9 characters after a string ignoring special characters?
考虑以下字符串:
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
它返回找到字符串的位置..但我不确定下一步如何进行。 谢谢
使用此regex
来识别模式。 也许它可以帮助:
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']
更新
import re
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'
regex = re.findall("([A-Z]{3})([A-Z0-9.\s\/]{2,})",str_test)
result = []
for r in regex:
result.append(r[0]+("".join(re.sub("([^\d\w])","",str(r[1])).replace("Z","2").replace("S","5"))))
print (result)
输出:
['NRC234456789', 'AZN123456789', 'BSA123456789', 'SSR789456123']
这是一种方法
前任:
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'
to_check = ['NRC', 'AZN', 'BSA', 'SSR']
pattern = re.compile("("+"|".join(to_check) + ")([\d+\.\s\/]+)")
for k, v in pattern.findall(str_test):
print(k + re.sub(r"[^\d]", "", v))
输出:
NRC234456789
AZN123456789
BSA123456789
SSR789456123
根据评论编辑。
import re
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'
to_check = ['NRC', 'AZN', 'BSA', 'SSR']
pattern = re.compile("("+"|".join(to_check) + ")([\d+\.\s\/ZS]+)")
for k, v in pattern.findall(str_test):
new_val = k + re.sub(r"[^\d]", "", v.replace("Z", "2").replace("S", "5"))
print(new_val)
这是首先使用此正则表达式查找预期文本的简单方法,
\b(?:NRC|AZN|BSA|SSR)(?:.?\d)+
使用提供的列表动态生成,然后从中删除任何非字母数字字符。
编辑:为了处理错误的字符串,其中2
被错误地写为Z
并且5
被错误地写为S
,您可以在忽略初始三个字符的字符串的第二部分替换它们。 此外,代码已更新,因此它只选择接下来的九位数字而不是更多数字。 这是我更新的相同 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
,
NRC234456789
AZN123456789
BSA123456789
BSA123456789
SSR789456123
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.