Python Regex Search And Replace

0 votes
I have been using Python since some time now but a complete newbie with regular expressions.

I am trying to use python re to convert a string such as

[Hollywood Holt](http://www.hollywoodholt.com)
to

<a href="http://www.hollywoodholt.com">Hollywood Holt</a>
 

and a string like

*Hello world*
to

<strong>Hello world</strong>
 

Can anyone help me? How do I do this?
May 20 in Python by ana1504.k
• 7,870 points
49 views

1 answer to this question.

0 votes
You need not use a regex. Your content is Markdown and you can take the string and run it through the markdown module.

First, make sure Markdown is installed. It has a dependancy on ElementTree so easy_install the two of them as follows. If you're running Windows, you can use the Windows installer instead.

easy_install ElementTree
easy_install Markdown
To use the Markdown module and convert your string to html simply do the following (tripple quotes are used for literal strings):

import markdown
markdown_text = """[Hollywood Holt](http://www.hollywoodholt.com)"""
html = markdown.markdown(markdown_text)
answered May 20 by SDeb
• 13,160 points

Related Questions In Python

+4 votes
6 answers

Substring search in Python

Use the "in" operator in python" if "substring" ...READ MORE

answered Apr 13, 2018 in Python by Nietzsche's daemon
• 4,260 points
84 views
0 votes
1 answer

How can I find out the index of an element from row and column in Python?

You probably want to use np.ravel_multi_index: [code] import numpy ...READ MORE

answered Apr 16, 2018 in Python by charlie_brown
• 7,720 points
67 views
0 votes
1 answer

what are "and" and "or" operators in Python?

AND - True if both the operands ...READ MORE

answered Apr 17, 2018 in Python by Johnathon
• 9,070 points
41 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 175 views
0 votes
1 answer

.replace() regex in Python

No,  .replace() does not support regex. Regular expressions ...READ MORE

answered Nov 26, 2018 in Python by SDeb
• 13,160 points
61 views
0 votes
1 answer

How is Python 2.7.3 and Python 3.3 different?

raw_input() is not used in Python 3. Use input()  ...READ MORE

answered Sep 12, 2018 in Python by SDeb
• 13,160 points
32 views