Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python replace string pattern with output of function

Tags:

python

regex

I have a string in Python, say The quick @red fox jumps over the @lame brown dog.

I'm trying to replace each of the words that begin with @ with the output of a function that takes the word as an argument.

def my_replace(match):     return match + str(match.index('e'))  #Psuedo-code  string = "The quick @red fox jumps over the @lame brown dog." string.replace('@%match', my_replace(match))  # Result "The quick @red2 fox jumps over the @lame4 brown dog." 

Is there a clever way to do this?

like image 997
nathancahill Avatar asked Sep 26 '12 08:09

nathancahill


People also ask

How do you replace a pattern in a string in Python?

To replace a string in Python, the regex sub() method is used. It is a built-in Python method in re module that returns replaced string. Don't forget to import the re module. This method searches the pattern in the string and then replace it with a new given expression.

How do you replace all occurrences of a regex pattern in a string in Python?

sub() method will replace all pattern occurrences in the target string. By setting the count=1 inside a re. sub() we can replace only the first occurrence of a pattern in the target string with another string. Set the count value to the number of replacements you want to perform.

How do you replace a pattern in a string?

To perform multiple replacements in each element of string , pass a named vector ( c(pattern1 = replacement1) ) to str_replace_all . Alternatively, pass a function to replacement : it will be called once for each match and its return value will be used to replace the match.


2 Answers

You can pass a function to re.sub. The function will receive a match object as the argument, use .group() to extract the match as a string.

>>> def my_replace(match): ...     match = match.group() ...     return match + str(match.index('e')) ... >>> string = "The quick @red fox jumps over the @lame brown dog." >>> re.sub(r'@\w+', my_replace, string) 'The quick @red2 fox jumps over the @lame4 brown dog.' 
like image 168
Janne Karila Avatar answered Oct 01 '22 06:10

Janne Karila


I wasn't aware you could pass a function to a re.sub() either. Riffing on @Janne Karila's answer to solve a problem I had, the approach works for multiple capture groups, too.

import re  def my_replace(match):     match1 = match.group(1)     match2 = match.group(2)     match2 = match2.replace('@', '')     return u"{0:0.{1}f}".format(float(match1), int(match2))  string = 'The first number is 14.2@1, and the second number is 50.6@4.' result = re.sub(r'([0-9]+.[0-9]+)(@[0-9]+)', my_replace, string)  print(result) 

Output:

The first number is 14.2, and the second number is 50.6000.

This simple example requires all capture groups be present (no optional groups).

like image 23
DaveL17 Avatar answered Oct 01 '22 06:10

DaveL17