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

Question:

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.

Asked By: rdil2503

||

Answers:

The b escape matches at word boundaries, but the $ sign is not considered part of word you can match. Match on the start or spaces instead:

re.compile(r'(?:^|s)($w+)')

I’ve used a backslash escape for the dollar here instead of a character class, and the w+ word character class with a minimum of 1 character to better reflect your intent.

Demo:

>>> import re
>>> dollaredwords = re.compile(r'(?:^|s)($w+)')
>>> dollaredwords.search('Here is an $example for you!')
<_sre.SRE_Match object at 0x100882a80>
Answered By: Martijn Pieters
>>> [word for word in mystring.split() if word.startswith('$')]
['$string', '$example']
Answered By: fraxel

The problem with your expr is that b doesn’t match between a space and a $. If you remove it, everything works:

z = 'This $string is an $example'
import re
print re.findall(r'[$]S*', z) # ['$string', '$example']

To avoid matching words$like$this, add a lookbehind assertion:

z = 'This $string is an $example and this$not'
import re
print re.findall(r'(?<=W)[$]S*', z) # ['$string', '$example']
Answered By: georg

Several approaches, depending on what you want define as a ‘word’ and if all are delineated by spaces:

>>> s='This $string is an $example $second$example'

>>> re.findall(r'(?<=s)$w+',s)
['$string', '$example', '$second']

>>> re.findall(r'(?<=s)$S+',s)
['$string', '$example', '$second$example']

>>> re.findall(r'$w+',s)
['$string', '$example', '$second', '$example']

If you might have a ‘word’ at the beginning of a line:

>>> re.findall(r'(?:^|s)($w+)','$string is an $example $second$example')
['$string', '$example', '$second']
Answered By: dawg

Using regex:

z = ‘This $string is an $example to$ get o$n$l$y words $sta$rts with $’

print(re.findall(r’s$[S]*’, z))

Output: [‘ $string’, ‘ $example’, ‘ $sta$rts’, ‘ $’]

Answered By: Nithesh
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.