Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regular expression - string replacement "as is"

Tags:

python

regex

I am trying to do this:

word test should be found in some text and be replaced with <strong>test</strong>. but the thing is, Test should be also catched and be replaced with <strong>Test</strong>.

I tried this:

word = "someword"
text = "Someword and many words with someword"
pattern = re.compile(word, re.IGNORECASE)
result = pattern.sub('<strong>'+word+'</strong>',text)

but in this case, Someword is becoming someword. Am I using re somehow wrong?

I want <strong>Someword</strong> and many words with <strong>someword</strong>

like image 532
doniyor Avatar asked May 13 '14 21:05

doniyor


People also ask

What does '$' mean in regex?

$ means "Match the end of the string" (the position after the last character in the string). Both are called anchors and ensure that the entire string is matched instead of just a substring.

What does \\ s+ mean in regex?

The plus sign + is a greedy quantifier, which means one or more times. For example, expression X+ matches one or more X characters. Therefore, the regular expression \s matches a single whitespace character, while \s+ will match one or more whitespace characters.

What is difference [] and () in regex?

[] denotes a character class. () denotes a capturing group. [a-z0-9] -- One character that is in the range of a-z OR 0-9. (a-z0-9) -- Explicit capture of a-z0-9 .

Can you replace text with regex?

Find and replace text using regular expressions When you want to search and replace specific patterns of text, use regular expressions. They can help you in pattern matching, parsing, filtering of results, and so on. Once you learn the regex syntax, you can use it for almost any language.


1 Answers

You need to use a capturing group:

>>> import re
>>> word = "someword"
>>> text = "Someword and many words with someword"
>>> pattern = re.compile('(%s)' % word, re.IGNORECASE)
>>> pattern.sub(r'<strong>\1</strong>',text)
'<strong>Someword</strong> and many words with <strong>someword</strong>'

Here \1 refers to the first captured group, to what was captured inside the parenthesis.

Also see Search and Replace section of the python re module docs.

like image 196
alecxe Avatar answered Oct 17 '22 22:10

alecxe