- 条件匹配:限定目标字符串前后的内容
限制匹配目标前的内容,注意顺序不能颠倒
re.search(r"(?<!num)\d+", "num123") # 结果为23
re.search(r"(?<!num)\d+", "123") # 结果为123
re.search(r"(?<=num)\d+", "num123") # 结果为123
re.search(r"(?<=num)\d+", "123") # 结果为None
限制匹配目标后的内容,注意顺序不能颠倒
re.search(r"\d+(?!num)", "123num") # 结果为12
re.search(r"\d+(?!num)", "123") # 结果为123
re.search(r"\d+(?=num)", "123num") # 结果为123
re.search(r"\d+(?=num)", "123") # 结果为None
作者:HC_JGJ
来源:CSDN
原文:https://blog.csdn.net/baidu_28289725/article/details/80414445
版权声明:本文为博主原创文章,转载请附上博文链接!