You can pass a function to re.sub()
that will allow you to do this, here is an example:
def upper_repl(match):
return 'GOO' + match.group(1).upper() + 'GAR'
And an example of using it:
>>> re.sub(r'foo([a-z]+)bar', upper_repl, 'foobazbar')
'GOOBAZGAR'
More Related Contents:
- Take first some character of string but if particular character will come consider that character also
- How to extract numbers from a string in Python?
- Is it worth using Python’s re.compile?
- Extract part of a regex match
- Regular expression matching a multiline block of text
- What is the difference between re.search and re.match?
- How to input a regex in string.replace?
- How can I do multiple substitutions using regex?
- regex error – nothing to repeat
- How to match any string from a list of strings in regular expressions in python?
- How to timeout function in python, timeout less than a second
- Split string based on regex
- Find all Chinese text in a string using Python and Regex
- How to use re to find consecutive, repeated chars
- How do I re.search or re.match on a whole file without reading it all into memory?
- python .replace() regex [duplicate]
- How to use regex to find all overlapping matches
- How can a recursive regexp be implemented in python?
- Regex, select closest match
- Python regular expression pattern * is not working as expected
- Split string at every position where an upper-case word starts
- FutureWarning: The default value of regex will change from True to False in a future version
- Why does this take so long to match? Is it a bug?
- Regex matching – why does this not match and return None?
- Why doesn’t ignorecase flag (re.I) work in re.sub() [duplicate]
- How to use str.contains() with multiple expressions in pandas dataframes
- match dates using python regular expressions
- How to Fix JSON Key Values without double-quotes?
- python regular expression across multiple lines
- How to use regex with optional characters in python?