简体   繁体   中英

use regex to parse string using python

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?

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])

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM