Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 846
rdil2503 Avatar asked Jul 10 '12 15:07

rdil2503


People also ask

How do you check if a word starts with a specific letter in Python?

The startswith() method returns True if a string starts with the specified prefix(string). If not, it returns False .

How do you search for a symbol in a string in Python?

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.

How do I find a specific word in a string Python?

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.


1 Answers

>>> [word for word in mystring.split() if word.startswith('$')]
['$string', '$example']
like image 110
fraxel Avatar answered Oct 04 '22 20:10

fraxel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!