pythonregexdollar-sign

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


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.


Solution

  • >>> [word for word in mystring.split() if word.startswith('$')]
    ['$string', '$example']