两点水
This commit is contained in:
30
Code/Python14Code/com/twowater/test7.py
Normal file
30
Code/Python14Code/com/twowater/test7.py
Normal file
@@ -0,0 +1,30 @@
|
||||
#!/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、先定义一个函数
|
||||
def convert(value):
|
||||
group = value.group()
|
||||
if (group == '*'):
|
||||
return '&'
|
||||
elif (group == '-'):
|
||||
return '|'
|
||||
|
||||
|
||||
# 第二个参数,要替换的字符可以为一个函数
|
||||
sub3 = re.sub('[\*-]', convert, a)
|
||||
print(sub3)
|
||||
Reference in New Issue
Block a user