1

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?

2 Answers 2

1

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])
Sign up to request clarification or add additional context in comments.

7 Comments

That helped. I modified original question to include another example. The solution provided worked for example 1 and 2, but did not work for 3.
For the third example, what would you like to extract?
for example 3, I want to extract 01372451.1 from the first row and 10552884 from the second row.
Please check one more example provided in original question...thanks for your help
There you go. Updated
|
1

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.