开发者

How do I do this regex in Python?

Suppose I have a string of text, of all characters Latin-based. With punctuation.

How do I "find" all the characters and put <strong> tags around it?

hay = The fox jumped up the tree.
needle = "umpe"

In this case, part of the word "ju开发者_如何学编程mped" would be highlighted.


Without regex (may be a bit more verbose but also easier to understand):

hay = "The fox jumped up the tree."
needle = "umpe"

print hay.replace(needle, "<strong>%s<strong>" % needle)

EDIT after extra specification: if you want case insensitive replace (which a regular string replace can't do):

import re

hay = "The fox jUMPed up the tree."
needle = "umpe"

regex = re.compile('(%s)' % needle, re.I)
print regex.sub('<strong>\\1</strong>', hay)


Using regular expressions on a simple search expression like this is overkill. However, in case you need a more complicated search, I referenced Python's re module documentation to put together the code below, which I think does what you want:

#!/usr/bin/env python
import re
haystack = "The fox jumped up the tree."
needle = "umpe"
new_text = "<strong>" + needle + "</strong>"
new_haystack = re.sub(needle, new_text, haystack)
print new_haystack


Your question is not very clear. If you want to highlight the words that have needle in them you can match

\b(\w*needle\w*)\b

and replace it with

<strong>\1<strong>


This should work:

pattern = r'(?P<needle>(umpe))'
pat_obj = re.compile(pattern)
new_text = pat_obj.sub(r'<strong>\g<needle></strong>', hay)

The result rendered in HTML: The fox jumped up the tree.

In the snippet above, i've used the re method 'sub' and referenced a captured group (which i called 'needle').


No regex used in this case, but will work for smaller strings.

hay = "The fox jumped up the tree."
needle = "umpe"

hay_lower = hey.lower()
found = []
curr_find = hay_lower.find(needle.lower())
found.append(curr_find)
hay_list = list(hay)

while(curr_find):
    curr_find = hay_lower.find(needle.lower(), curr_find)

for found_index in found:
   hay_list[found_index:found_index+len(needle)] = '<strong>%s</strong>' % needle

result = ''.join(hay_list)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜