-1

I have string:

Lorem ipsum dolor [mytag]something[/mytag]sit amet, ipsum [mytag]something else[/mytag]a laoreet ultricies

and I want to get values "something" and "something else" and replace it.

How to do it?

2
  • 2
    i don't know why this question got a up-vote! it doesn't say what's the expected output. And no sample code from the OP. Commented May 22, 2012 at 12:29
  • It's post. I want to replace tags [img] [/img] with <img src="..." /> Commented May 22, 2012 at 12:30

2 Answers 2

3

You can use the bbcode module :

# Using the default parser.
import bbcode
html = bbcode.render_html(text)

Optionnal because i think there is already [img] :

# Installing simple formatters.
parser = bbcode.Parser()
parser.add_simple_formatter('img', '<img src="%(value)" />')
Sign up to request clarification or add additional context in comments.

Comments

1

Regular expressions is the way to go.

import re
s = 'Lorem ipsum dolor [mytag]something[/mytag]sit amet, ipsum [mytag]something else[/mytag]a laoreet ultricies'
print re.sub(r'\[mytag\](.+?)\[/mytag\]', r'<img src="\1">', s)

2 Comments

Your solution works, but Python bbcode module is more elegant :-)
@Yohann: I agree, but installing third-party modules can be inappropriate for the OP.

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.