擅长:python、mysql、java
<p>最简单的方法是re.sub公司()python regex模块中的方法,re。在</p>
<pre><code> import re
def replace_rule(string, new, pattern):
return re.sub(pattern, new, string)
def replace_example(string):
pattern = r"100"
replace_with = "1"
return re.sub(pattern, replace_with, string)
replace_example("1009")
=> '19'
replace_example("1009100")
=> '191'
</code></pre>
<p>Regex是一种将字符串与某些正则模式匹配的方法,并对它们执行某些操作,如sub,它查找并替换字符串中的模式。这里有一个链接:<a href="https://docs.python.org/3/library/re.html" rel="nofollow">https://docs.python.org/3/library/re.html</a></p>