Find all words in a string that start with the $ sign in Python

rdil2503 picture rdil2503 · Jul 10, 2012 · Viewed 19.9k times · Source

How can I extract all words in a string that start with the $ sign? For example in the string

This $string is an $example

I want to extract the words $string and $example.

I tried with this regex \b[$]\S* but it works fine only if I use a normal character rather than dollar.

Answer

fraxel picture fraxel · Jul 10, 2012
>>> [word for word in mystring.split() if word.startswith('$')]
['$string', '$example']