import re
personal_pronoun = "se les" #example 1
personal_pronoun = "se le" #example 2
personal_pronoun = "se le" #example 3
personal_pronoun = "les" #example 4
personal_pronoun = "le" #example 5
#re.match() only matches at the beginning of the string
if re.match(r"se", personal_pronoun):
#concatenate this regex "negative look behind" to make a conditional negative match
personal_pronoun_for_regex = re.sub(r"^se", r"(?<!se\s)se", personal_pronoun)
else:
personal_pronoun_for_regex = personal_pronoun
#re.search() searches for matches anywhere in the string.
if re.search(r"\s*le$", personal_pronoun_for_regex):
#concatenate the \b metacharacter representing a word boundary
personal_pronoun_for_regex = re.sub(r"le$", r"le\b", personal_pronoun_for_regex)
#I check how the raw string looks like before using it in a regex
print(repr(personal_pronoun_for_regex)) # --> output raw string
This code give me that error raise s.error('bad escape %s' % this, len(this)) re.error: bad escape \s at position 6
What could I do to get these raw strings into the personal_pronoun_for_regex
variable without having these re
errors?
I think this is because there is an error within the re.sub()
functions, causing a re.error
object to be raised indicating that there was a problem processing the replacing regular expression.
This is how the raw string, so that special characters are interpreted literally as part of the regular expression, should actually look like:
personal_pronoun_for_regex = r"se les" #for example 1
personal_pronoun_for_regex = r"se le\b" #for example 2
personal_pronoun_for_regex = r"se le\b" #for example 3
personal_pronoun_for_regex = r"(?<!se\s)les" #for example 4
personal_pronoun_for_regex = r"(?<!se\s)le\b" #for example 5