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

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)

Leave a Reply