Can I create a RegEx set in containing meta-characters?

Viewed 62

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.

3 Answers

You can use the following regex which adds an or (|) case for the end condition:

'a\wcd[ \.\n]|a\wcd$'

Example:

import re

text = "abcd aacd. abcd\n adcd"

my_re = re.compile(r'a\wcd[ \.\n]|a\wcd$')
pat_iter = my_re.finditer(text)

for i in pat_iter:
    print(i.group())

# abcd
# aacd.
# abcd

# adcd

You might use

\ba\wcd[.,]?\s*

In parts

  • \ba Match wordboundary followed by a
  • \w Match a word character
  • cd Match cd literally
  • [.,]? Match an optional . or ,
  • \s* Match 0+ whitespace chars which will also match newlines

Regex demo | Python demo

For example

import re
text = "abcd aacd. abcd\n adcd"

my_re = re.compile(r'\ba\wcd[.,]?\s*')
pat_iter = my_re.finditer(text)

for i in pat_iter:
    print(i.group())

Output

abcd 
aacd. 
abcd

adcd
Related