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

Regular expression to extract text in python

I am new using the re library and I would like to know if somebody knows how to extract the following text:

Initial

'[p]I am a test paragraph[/p]'

Output

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

I am a test paragraph

I tried to use the following line :

text = '[p]I am a test paragraph[/p]'
param = re.findall("[p](.*?)[/p]]", text)

but the output was :

>>[']I am a test paragraph[/']

I tried to used the BBCode library but it doesn’t work with this kind of text.

>Solution :

# regex to extract text between [p] and [/p] tags
regex = r'\[p\](.*?)\[/p\]'
test_text = '[p]I am a test paragraph[/p]'

# extract text between [p] and [/p] tags
list_of_results = re.findall(regex, test_text)
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