Reputation: 10249
Is there a way to do substitution on a group?
Say I am trying to insert a link into text, based on custom formatting. So, given something like this:
This is a random text. This should be a [[link somewhere]]. And some more text at the end.
I want to end up with
This is a random text. This should be a <a href="/link_somewhere">link somewhere</a>. And some more text at the end.
I know that '\[\[(.*?)\]\]'
will match stuff within square brackets as group 1, but then I want to do another substitution on group 1, so that I can replace space with _
.
Is that doable in a single re.sub
regex expression?
Upvotes: 1
Views: 279
Reputation: 70722
You could do something like this.
import re
pattern = re.compile(r'\[\[([^]]+)\]\]')
def convert(text):
def replace(match):
link = match.group(1)
return '<a href="{}">{}</a>'.format(link.replace(' ', '_'), link)
return pattern.sub(replace, text)
s = 'This is a random text. This should be a [[link somewhere]]. .....'
convert(s)
See working demo
Upvotes: 1
Reputation: 368954
You can use a function as a replacement instead of string.
>>> import re
>>> def as_link(match):
... link = match.group(1)
... return '<a href="{}">{}</a>'.format(link.replace(' ', '_'), link)
...
>>> text = 'This is a random text. This should be a [[link somewhere]]. And some more text at the end.'
>>> re.sub(r'\[\[(.*?)\]\]', as_link, text)
'This is a random text. This should be a <a href="link_somewhere">link somewhere</a>. And some more text at the end.'
Upvotes: 3