Search code examples
regexpython-3.xdata-extraction

Regex in python to extract certain codes


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?


Solution

  • You can use this regex

    pattern = r'[A-Z]+\d+(\.\d+)?(\w+)?'