Home > Software design >  Finding strings (including its special character) after keyword in python
Finding strings (including its special character) after keyword in python

Time:03-20

Suppose I have a text that looks like this

text = No. ADJ.TRM/085/BFG/2015

the output that I wanted is ADJ.TRM/085/BFG/2015

I have tried using

re.search('(?<=No. )(\w )',text)

It gives me only ADJ

also tried

re.search('No.\s\w \s\w \s(\w )',text)

It gives me an error

CodePudding user response:

if match := re.search("No\. (.*)", text):
    print(match.group(1))

CodePudding user response:

It rather depends on the exact format of your string but, as given, you can simply do this:

text = 'No. ADJ.TRM/085/BFG/2015'

print(text.split()[1])

Output:

ADJ.TRM/085/BFG/2015

CodePudding user response:

You can use findall or search like below:

import re

text = 'No. ADJ.TRM/085/BFG/2015'

try :
    print(re.findall('(?<=No. )(. )',text)[0])
    # or
    # print(re.search('(?<=No. )(. )',text).group(1))
    
except IndexError:
    print('Not Found')

Output:

'ADJ.TRM/085/BFG/2015'
  • Related