in thePythonDevelopment Find and replace is very simple if the current object is a string Str , you can use the type provided by the Find () or index () method finds the specified character, returns the first occurrence of the character if it is found, and returns 1 if it does not exist .
>>> s = ' Cat and dog ' >>> s.find (' dog ') 8>>> s.index (' dog ') 8>>> s.find (' Duck ')
-1
If you want to replace the target string, use the replace () the method is good.
>>> s = ' cat and dog ' >>> s.replace (' Cat ', ' dog ') ' Dog and dog '
wildcard lookup matches
of course, if you feel that the above features are not enough for you, do you want to use wildcards to find strings? No problem! fnmatch This library will be able to meet your requirements, see examples!
>>> s = ' Cat and Dog ' >>> import fnmatch>>> fnmatch.fnmatch (S, ' cat* ')
true>>> Fnmatch.fnmatch (S, ' c*and*d? ')
false>>> Fnmatch.fnmatch (S, ' c*and*d* ')
True
Regular expression lookup substitution
If you need to find more complex character rules, the regular expression is your choice. The following is a simple example of a regular lookup.
>>> Import re>>> s = ' We'll fly to Thailand on 2016/10/31 ' >>> pattern = R ' \\d+ ' >>> re.findall (patte RN, s)
["], ' ten ', ' + ']>>> re.search (pattern, s)
<_sre.sre_match object= "" at= "" 0x03a8fd40= "" >>>> re.search (pattern, s). Group () '
next you may need to replace certain characters with regular expressions, so you need to know re.sub () method, see example.
>>> s = "I like {color} car." >>> re.sub (R ' \\{color\\} ', ' Blue ', s) ' I like blue car. '
< Span style= "FONT-FAMILY:CALIBRI;FONT-SIZE:16PX;" > >>> s = ' We'll fly to Thailand on 10/31/2016 ' >>> re.sub (' (\\d+)/(\\d+)/(\\d+) ', R ' \\3-\\1-\\2 ', s) ' W E would fly to Thailand on 2016-10-31 '
< Span style= "FONT-FAMILY:CALIBRI;FONT-SIZE:16PX;" > Far more powerful than you are. In the example above you can replace the {color} such template characters, You can also match the regular to all the grouping order, for example, the second example matches the 3 r ' \\3-\\1-\\2 ' Are you clear?
Next look at another example.
s = "Tom is talking to Jerry."
name1 = "Tom"
name2 = "Jerry"
pattern = R ' (. *) ({0}) (. *) ({1}) (. *) '. Format (name1, name2) Print re.sub (pattern, R ' \\1\\4\\3\\2\\5 ', s) # Jerry was talking to Tom.
In fact, you can also customize the replacement function, i.e. re.sub () the second parameter.
defchange_date (m):
from calendar import month_abbr
mon_name = Month_abbr[int (M.group (1))]
return ' {} {} {} '. Format (M.group (2), Mon_name, M.group (3))
s = ' We'll fly to Thailand on 10/31/2016 '
pattern = R ' (\\d+)/(\\d+)/(\\d+) ' Print re.sub (pattern, change_date, s) # We'll fly to Thailand on OCT
finally give you a final version of the example, the use of the function of the closure, acid cool, you understand!
defmatch_case (word):
Defreplace (m):
text = M. Group ()
if text. Isupper ():
return word.upper ()
elif text . Islower ():
return word.lower ()
elif text [0].isupper ():
return word.capitalize ()
Else:
return word
return replace
s = " love python, Love python, Love Python"
print re. Sub (' Python ', match_case (' money '), S, Flags=re. IGNORECASE) # Love Money
written in the last
In fact, there are many ways to play the regular expression, if you want to mix the regular and wildcard, a little problem is not, because Fnmatch There 's another one. Translate () method that allows you to convert the wildcard to a regular expression, and you can play as much as you like.
>>> fnmatch.translate (' c*and*d* ') ' c.*and.*d.* '
Source: Test Meow
How do I find and replace text in Python?