简体   繁体   English

Python正则表达式搜索和替换

[英]Python Regex Search And Replace

I'm not new to Python but a complete newbie with regular expressions (on my to do list) 我不是Python的新手,而是一个有正则表达式的完整新手(在我的待办事项列表中)

I am trying to use python re to convert a string such as 我试图使用python re来转换字符串,如

[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>

Why are you bothering to use a regex? 你为什么要使用正则表达式? Your content is Markdown, why not simply take the string and run it through the markdown module? 您的内容是Markdown,为什么不简单地使用字符串并通过降价模块运行它?

First, make sure Markdown is installed. 首先,确保安装了Markdown。 It has a dependancy on ElementTree so easy_install the two of them as follows. 它依赖于ElementTree,因此easy_install它们中的两个如下。 If you're running Windows, you can use the Windows installer instead. 如果您运行的是Windows,则可以使用Windows安装程序

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): 要使用Markdown模块并将您的字符串转换为html,只需执行以下操作(tripple引号用于文字字符串):

import markdown
markdown_text = """[Hollywood Holt](http://www.hollywoodholt.com)"""
html = markdown.markdown(markdown_text)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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