bkula
bkula

Reputation: 569

Match only final character of regex

I have some Python code that involves a lot of re.sub() commands. In some cases, I want to replace a character but only if it comes after certain other characters. The following is an example of how I currently am doing this in python:

secStress = "[aeiou],"[-1]

So my input for this would be a string like "a,s I walk, I hum." And I want to replace that first comma but not the "a" that comes before it.

The problem is that Python doesn't like when I give it a variable as input for re.sub(). Is there a way I can write a regex that specifies that only its final character is supposed to be matched?

Upvotes: 2

Views: 331

Answers (2)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627468

You are looking for either a capturing group/backreference or a positive lookbehind solution:

s = "a,s I walk, I hum."

# Capturing group / backreference
print(re.sub(r"([aeiou]),", r"\1", s))

# Positive lookbehind
print(re.sub(r"(?<=[aeiou]),", "", s))

See the Python demo.

First approach details

The ([aeiou]) is a capturing group that matches a vowel and stores it in a special memory buffer that you can refer to from the replacement pattern using backreferences. Here, the Group ID is 1, so you can access that value using r"\1".

Second approach details

The (?<=[aeiou]) is a positive lookbehind that only checks (but does not add the text to the match value) if there is a vowel immediately before the current position. So, only those commas are matched that are preceded with a vowel and it is enough to replace with an empty string to get rid of the comma since it is the only symbol kept in the match.

Upvotes: 2

Bill Bell
Bill Bell

Reputation: 21663

If I understand you correctly,

>>> import re
>>> def doit(matchobj):
...     return matchobj.group()[0]
... 
>>> re.sub(r'[aeiou],', doit, "a,s I walk, I hum.")
'as I walk, I hum.'

If the regex matches then doit is called with the object that matched. Whatever string doit returns (and it must be a string) is put in place of the match.

Upvotes: 0

Related Questions