?? ????? ???? ??? ???? ?? ??? ????. ?? def sub(pattern, repl, string, count=0, flags=0) ??? ???? ? 5?? ????? ????. . ? ?? ?? ????: ??, repl, ???; ? ?? ??? ????: count, flags
???? ???? ??? ??? ????.
Description | pattern | ?? ???? ?? ???? ?????.
| repl | repl? ????, ??? ???? ??
| string | ? ?? ? ??? ???? ?????.
| count | For ???? ???? ??, ??? ?? ? ??? ??? ??? ? ????.
| flags | ?? ??? ???
| ???? ???? ?? ?? ?????. ?? ?? ?? ???? ???????. ???? ? ??? ?? ? ?? ????? ??? ??? ? ??? ????. ?? ??, ??? ?? ??? ??? ??? ?????. | #!/usr/bin/env python3
# -*- coding: UTF-8 -*-
import re
a = 'Python*Android*Java-888'
# 把字符串中的 * 字符替換成 & 字符
sub1 = re.sub('\*', '&', a)
print(sub1)
# 把字符串中的第一個 * 字符替換成 & 字符
sub2 = re.sub('\*', '&', a, 1)
print(sub2)
# 把字符串中的 * 字符替換成 & 字符,把字符 - 換成 |
# 1、先定義一個函數(shù)
def convert(value):
group = value.group()
if (group == '*'):
return '&'
elif (group == '-'):
return '|'
# 第二個參數(shù),要替換的字符可以為一個函數(shù)
sub3 = re.sub('[\*-]', convert, a)
print(sub3)
?? ??: Python&Android&Java-888
Python&Android*Java-888
Python&Android&Java|888
?? ????? ????? ? ????. ?? ???? ???? ????. ???? ? ??? ?? ?? ??????~
? ??? ??? ???? ???? ????.