1

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?

2 Answers 2

3

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.'
Sign up to request clarification or add additional context in comments.

Comments

1

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

2 Comments

Yes, and I think this will not work if there are more than two words within the link.
Updated with better answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.