Extract words from a string of text with regex

Let time show you how to extract words from a text that has some tags into it, for many different purposes.
We have this text:
Oltre alla funzione creditizia, le banche svolgono la funzione monetaria, in quanto creano strumenti di tipo cartaceo che sostituiscono il denaro come gli assegni e le cambiali.

I add some tags to the words I want.
Oltre alla funzione creditizia, le banche svolgono la funzione #monetaria, in quanto creano strumenti di tipo cartaceo che sostituiscono il denaro come gli #assegni e le #cambiali.

To grab the text we will use the re module and we will memorize the text into a variable calles es1.

import re

es1 = "Oltre alla funzione creditizia, le banche svolgono la funzione #monetaria, in quanto creano strumenti di tipo cartaceo che sostituiscono il denaro come gli #assegni e le #cambiali."

Now we create a list of the words starting with #

wl = re.findall("#[a-zA-z0-9]+", es1)

With this code, using findall function of re module, we grab a list of the words starting with ‘#’ and followed by any letter or number untile a space.
Now I can use this list for my purpose.
If I want to get rid of the ‘#’ into the es1 string variable, I can do this:

es1 = es1.replace("#","")

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.