Reputation: 15
I am developing a sample code for my customer and haven't done programming in Python before. How do I use regex to extract text below?
RSH|^~\&|MIC|SCC|MS4OD|A|201804060738||ORU^R01|0137245|P|2.2
KID|||10552884||Fer^Huh^GRV||20223164433|M||W|193 Biop Mill
I want to extract 0137245 from the first row and 10552884 from the second row.
RSH|^~\&|MIC|SCC|MS4OD|A|201804060738||ORU^R01|0137246|P|2.3
KID|||10552885||Fer^Huh^GRV||2023164434|M||W|193 jason Mill
I want to extract 0137246 from the first row and 10552885 from the second row.
Example 3
RSH|^~\&|MIC|SCC|MS4OD|A|201804060738||ORU^R01|01372451.1|P|2.2
KID|1||10552884||Fer^Huh^GRV||20223164433|M||W|193 Biop Mill
I want to extract 01372451.1 from the first row and 10552884 from the second row.
Example 4
RSH|^~\&|MIC|SCC|MS4OD|A|201804060738||ORU^R01|01372451.1|P|2.2
KID|1|2|10552884||Fer^Huh^GRV||20223164433|M||W|193 Biop Mill
I want to extract 01372451.1 from the first row and 10552884 from the second row.
Any suggestions?
Upvotes: 1
Views: 264
Reputation: 6740
Based on the examples you've given, you could use
regex1=re.compile('\|\|ORU\^R01\|(\d+(?:\.\d+)?)\|')
regex2=re.compile('^KID\|\d?\|\d?\|(\d+)\|')
match1=regex1.search(firstRow)
if match1 is not None:
print(match1.groups()[0])
match2=regex2.search(secondRow)
if match2 is not None:
print(match2.groups()[0])
Upvotes: 1
Reputation: 2615
this is the solution to get the numbers from both the rows or any number of rows
str = 'RSH|^~\&|MIC|SCC|MS4OD|A|201804060738||ORU^R01|0137246|P|2.3KID|||10552885||Fer^Huh^GRV||2023164434|M||W|193 jason Mill '
res = re.findall(r'\b\d{7,8}\b',str)
print(res)
Upvotes: 1