目录
1.3 HiveSQL中查询某个字段是否包含手机号并做替换操作
1.2 python中使用正则表达式来匹配不包含某个字符串的行
1. 匹配文本中手机号
1.1 模式说明
pattern = r"^1[3-9]\d{9}$"
说明:
这个正则表达式的意思是以数字1开头,后面跟着3-9之间的数字,再加上任意9个数字,总共11位数字。这个正则表达式可以匹配大部分中国大陆手机号码。
1.2 python中验证手机号是否合法
import re
phone_number = "13812345678"
pattern = r"^1[3-9]\d{9}$"
if re.match(pattern, phone_number):
print("手机号码合法")
else:
print("手机号码不合法")
1.3 HiveSQL中查询某个字段是否包含手机号并做替换操作
select
regexp_replace(json,'desmobile:1[3-9][0-9]{9}','desmobile:') -- 替换手机号
from tableName
where ds='20230411'
and regexp_like(json, 'desmobile:1[3-9][0-9]{9}') -- 匹配包含手机号的记录
limit 2
2. 匹配不包含某个字符串的行
1.1 模式说明
^(?!.*pattern).*
其中,`pattern`是你想要排除的字符串。这个正则表达式的意思是:匹配任意行,但是这些行不能包含`pattern`字符串。
1.2 python中使用正则表达式来匹配不包含某个字符串的行
import re
text = """This is a line
This is another line
This line contains the word apple
This line does not contain the word banana
"""
pattern = r"^(?!.*banana).*$"
for line in text.split("\n"):
if re.match(pattern, line):
print(line)
这个代码会输出以下结果:
```
This is a line
This is another line
This line contains the word apple
```
可以看到,只有不包含`banana`字符串的行被匹配到了。