I have corpus made up of thousands of strings. I want to run a regular expression over each string to find the beginning and ending of specific words, including any punctuation.
I have tried to write a RegEx which includes a set containing a number of possible word beginnings/endings including regular characters like space, comma, full stop, etc. It also needs to include meta-characters like new line and string-end.
Here is an example of a string and the kind of regular expression I've been trying to use:
text = "abcd aacd. abcd\n adcd"
my_re = re.compile(r'[ ^]a\wcd[ .,$]')
pat_iter = my_re.finditer(text)
for i in pat_iter:
print(i.group())
The output I would like to get from this would be:
abcd
aacd.
abcd
adcd
Unfortunately, I can't use meta-characters like ^, \n and $ within a square-bracket set, [].
Is there any way to un-escape these characters within a set so that they can be used as meta-characters?
Edit: I don't want to separate these words using something like splitting wherever there is a space, like:
text = text.split(" ")
It needs to be with a RegEx like I described above.