I have a sentence. I want to find all occurrences of a word that start with a specific character in that sentence. I am very new to programming and Python, but from the little I know, this sounds like a Regex question.
What is the pattern match code that will let me find all words that match my pattern?
Many thanks in advance,
Brock
re.match() function of re in Python will search the regular expression pattern and return the first occurrence. The Python RegEx Match method checks for a match only at the beginning of the string. So, if a match is found in the first line, it returns the match object.
Search multiple words using regex Use | (pipe) operator to specify multiple patterns.
import re
print re.findall(r'\bv\w+', thesentence)
will print every word in the sentence that starts with 'v'
, for example.
Using the split
method of strings, as another answer suggests, would not identify words, but space-separated chunks that may include punctuation. This re
-based solution does identify words (letters and digits, net of punctuation).
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