Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace an re match with a transformation of that match?

Tags:

python

regex

For example, I have a string:

The struct-of-application and struct-of-world

With re.sub, it will replace the matched with a predefined string. How can I replace the match with a transformation of the matched content? To get, for example:

The [application_of_struct](http://application_of_struct) and [world-of-struct](http://world-of-struct)

If I write a simple regex ((\w+-)+\w+) and try to use re.sub, it seems I can't use what I matched as part of the replacement, let alone edit the matched content:

In [10]: p.sub('struct','The struct-of-application and struct-of-world')
Out[10]: 'The struct and struct'
like image 321
KIDJourney Avatar asked Aug 18 '16 04:08

KIDJourney


People also ask

How do I replace only part of a match with Python re sub?

Put a capture group around the part that you want to preserve, and then include a reference to that capture group within your replacement text. @Amber: I infer from your answer that unlike str. replace(), we can't use variables a) in raw strings; or b) as an argument to re. sub; or c) both.

Does re sub replace all occurrences?

By default, the count is set to zero, which means the re. sub() method will replace all pattern occurrences in the target string.

How do you replace a section of a string in regex?

To use RegEx, the first argument of replace will be replaced with regex syntax, for example /regex/ . This syntax serves as a pattern where any parts of the string that match it will be replaced with the new substring. The string 3foobar4 matches the regex /\d. *\d/ , so it is replaced.

What is the difference between re match () and re search ()?

There is a difference between the use of both functions. Both return the first match of a substring found in the string, but re. match() searches only from the beginning of the string and return match object if found.


2 Answers

Use a function for the replacement

s = 'The struct-of-application and struct-of-world'
p = re.compile('((\w+-)+\w+)')
def replace(match):
    return 'http://{}'.format(match.group())
    #for python 3.6+ ... 
    #return f'http://{match.group()}'

>>> p.sub(replace, s)

'The http://struct-of-application and http://struct-of-world'
>>>
like image 180
wwii Avatar answered Oct 08 '22 02:10

wwii


Try this:

>>> p = re.compile(r"((\w+-)+\w+)")
>>> p.sub('[\\1](http://\\1)','The struct-of-application and struct-of-world')
'The [struct-of-application](http://struct-of-application) and [struct-of-world](http://struct-of-world)'
like image 36
Nehal J Wani Avatar answered Oct 08 '22 02:10

Nehal J Wani