Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

extract alpha numeric id

I am looking to use RE to extract an id and description from the input which is in following format:

TTTT.1.A8This is important
AA.1.2.2ANothing is sometimes important
AAC.1A.2Everything sometimes is not important

Expected result:

ID         description
TTTT.1.A8  This is important
AA.1.2.2A  Nothing is sometimes important
AAC.1A.2   Everything sometimes is not important

I tried to achieve it as below:
img1 =  re.compile(r"\w+\.\d+")

for in in input:

   if re.search(img1,i.text):    
     req_id = str.strip(re.search(img1,i.text).group(0))
    
     req_text = str.strip(re.split(img1,i.text)[1])
     control_ids[req_id] = req_text

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Find an upper case letter followed by a lower case letter:

/([A-Z][a-z])/g

then replace it as a space and itself $1 for JavaScript \g<1> for Python.

JavaScript: Regex101

Python: Regex101

const str =`TTTT.1.A8This is important
AA.1.2.2ANothing is sometimes important
AAC.1A.2Everything sometimes is not important`;

const rgx = new RegExp(/([A-Z][a-z])/, 'g');

const output = str.replace(rgx, ` $1`);

console.log(output);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading