简体   繁体   中英

Replace brackets around words with something else using regex in Python

I have this text:

Lorem ipsum [!one] and [!two]

And I need to get to this text:

Lorem ipsum [one](http://example.com/one) and [two](http://example.com/two)

This code finds each word between "[!" and "]"

import re
r = r"\[\!(\w+)\]"

text = "Lorem ipsum [!one] and [!two]"

result = re.findall(r, text)

This gives me the following result

['one', 'two']

I could use .replace() but I was wondering if this is doable with regex.

Edit:

I needed the matched text to be processed a bit before replacing it. This is the solution using the answer as a starting point:

import re

def generate_url(input):
    # Do extra stuff here
    return "http://example.com/%s" % input

input = '''Lorem ipsum [!one] and [!two]'''
regex = "\[@([^]]+)\]"

url_generator = lambda match: "[%s](%s)" % (match.group(1), generate_url(match.group(1)))

output= re.sub(regex, url_generator, input)

You can use re.sub() fro this purpose.

input = '''Lorem ipsum [!one] and [!two]'''
input = re.sub("\[!([^]]+)\]", '[\\1](http://example.com/\\1)', input)

\\\\1 is the captured group from the regex matching ([^]]+)

You can use re.sub() :

>>> import re
>>> s = "Lorem ipsum [!one] and [!two]"
>>> re.sub(r"\[\!(\w+)\]", r'[\1](http://example.com/\1)', s)
'Lorem ipsum [one](http://example.com/one) and [two](http://example.com/two)'

\\1 is a reference for the captured group (\\w+) .

Also see documentation on capturing groups.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM