Home > Blockchain >  extract alpha numeric id
extract alpha numeric id

Time:06-30

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

CodePudding user response:

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

  • Related