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.
The startswith() method returns True if a string starts with the specified prefix(string). If not, it returns False .
Method 1: Get the position of a character in Python using rfind() Python String rfind() method returns the highest index of the substring if found in the given string. If not found then it returns -1.
Python String find() method returns the lowest index or first occurrence of the substring if it is found in a given string. If it is not found, then it returns -1.
>>> [word for word in mystring.split() if word.startswith('$')]
['$string', '$example']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With