0

I want to make a simple home made bbcode.

Example of a string :

s = 'Hello world, this is my website {url}myURL{/url}'

I want to replace {url}myURL{/url} by <a href="myURL">myURL</a>

It will be great if someone has the solution ?

2

2 Answers 2

6

Using re.sub

Ex:

import re

s = 'Hello world, this is my website {url}myURL{/url}'
print(re.sub(r"{url}(.*?){\/url}", r'<a href="\1">\1</a>', s))

Output:

Hello world, this is my website <a href="myURL">myURL</a>
Sign up to request clarification or add additional context in comments.

Comments

0

Python String replace() Method

The replace() method replaces a specified phrase with another specified phrase.

Note: All occurrences of the specified phrase will be replaced, if nothing else is specified.

Syntax:

string.replace(oldvalue, newvalue, count)

s = 'Hello world, this is my website {url}myURL{/url}'
print (s.replace('{url}', '<a href="').replace('{/url}','">myURL</a>'))

output:

Hello world, this is my website <a href="myURL">myURL</a>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.