import re

'''
\w匹配包括下划线在内任何字母数字字符,它相当于类[a-zA-Z0-9]
\W匹配非任何字母数字字符包括下划线在内,它相当于类[^a-zA-Z0-9
]
?P<>定义组里匹配内容的key(键),<>里面写key名称,值就是匹配到的内容
'''
#

str1 = "123 is 456"

str2 = re.sub("\d+", "5555", str1)

print(str2)

inputStr = "hello crifan, nihao crifan";
replacedStr = re.sub(r"hello (\w+), nihao \1", "crifanli", inputStr);
print(replacedStr)

inputStr = "hello crifan, nihao crifan";
replacedStr = re.sub(r"hello (\w+), nihao \1", "\g<1>", inputStr);
print(replacedStr)

其中\g<1>表示组1

inputStr5 = "hello victor is a good man";
replacedStr5 = re.sub(r"(.+?)", "w", inputStr5);
print("vitor is :", replacedStr5)

if re.search("ob", inputStr5):
print("find it!")

if re.match("hello", inputStr5):
print("matched!")

def pythonReSubDemo():
"""
demo Pyton re.sub
"""
inputStr = "hello 123 world 456";

def _add111(matched):
    intStr = matched.group("number"); #123
    intValue = int(intStr);
    addedValue = intValue + 111; #234
    addedValueStr = str(addedValue);
    return addedValueStr;

replacedStr = re.sub("(?P\d+)", _add111, inputStr);
print(replacedStr) #hello 234 world 567

pythonReSubDemo();

str3 = " **abc is abc*****"
str4 = " "
print(str4.strip())
if str4.strip():
print("not null")
else:
print("str4 is null")
print(str3)
print(str3.strip())