I am new to regex, I have the following expressions, I want to find the word or consecutive words ending with colon(:) using regular expression.
Incarcerate: imprison or confine, Strike down: if someone is struck down, especially by an illness, they are killed or severely harmed by it, Accost: approach and address.
The output should be like this Incarcerate:, Strike down:, Accost:
. I have written the following regex, but it captures the following.
My regex -> (\w+):+
It captures the words like Incarcerate:, Accost:
, it does not capture Strike down:
Please help me.
I want to do it in both typescript and python.
You can optionally repeat a space and 1+ word chars. Note that the words are in group 1, and the :
is outside of the group.
(\w+(?: \w+)*):
To include the :
in the match:
\w+(?: \w+)*:
The pattern matches
\w+
Match 1 or more word characters(?: \w+)*
Repeat 0+ times matching a space and 1+ word characters:
Match a single :
Example in Python
import re
s = "Incarcerate: imprison or confine, Strike down: if someone is struck down, especially by an illness, they are killed or severely harmed by it, Accost: approach and address."
pattern = r"\w+(?: \w+)*:"
print(re.findall(pattern, s))
Output
['Incarcerate:', 'Strike down:', 'Accost:']