0

I have written a regex in python to extract codes like:

I63.9 J45.909 M18.90 Z82.61 Z82.389 A030 A029 S87.02XD H4010X2 S12530K V675XXS

The regex which I am using is shown below:

import re
data="We have the following codes to extract, I63.9 J45.909 M18.90 Z82.61 Z82.389 A030 A029 S87.02XD H4010X2 S12530K V675XXS September 2018"
regular_expression=re.compile(r'[a-zA-Z]\d{1,2}\.*\d{1,3}\w{0,2}',re.I)
result=value_1.findall(data)
print(result)

Can someone tell me if it is the perfect regex to extract these codes or what could be a better and more robust regex to extract the above codes?

8
  • This seems like a job better suited for Split() Commented Jan 18, 2019 at 16:13
  • is the requirement for code: an alphabet followed by 1 or 2 digits followed by 0 to n . followed by 1, 2 or 3 digits followed by 0,1 or 2 characters? none of the codes given in the sample needs the last \w{0,2} part of the regex Commented Jan 18, 2019 at 16:20
  • @Matt.G sorry for the confusion. Updated my question. There could also be codes like: S87.02XD H4010X2 S12530K V675XXS Commented Jan 18, 2019 at 16:53
  • @RobertHarvey I did not get you? Commented Jan 18, 2019 at 16:54
  • python-reference.readthedocs.io/en/latest/docs/str/split.html Commented Jan 18, 2019 at 17:12

1 Answer 1

2

You can use this regex

pattern = r'[A-Z]+\d+(\.\d+)?(\w+)?'
Sign up to request clarification or add additional context in comments.

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.