# encoding: utf-8
# 版权所有 2024 ©涂聚文有限公司
# 许可信息查看:
# 描述: 正则表达式用法
# Author : geovindu,Geovin Du 涂聚文.
# IDE : PyCharm 2023.1 python 311
# Datetime : 2024/4/28 10:37
# User : geovindu
# Product : PyCharm
# Project : EssentialAlgorithms
# File : RegularString.py
# explain : 学习
import re
import os
import sys
import string
class DuString(object):
"""
正则表达式用法
"""
@staticmethod
def strSplitUper(textSource: str, patterns: str) -> list:
"""
分割字符串 第一个字母大写
:param textSource:
:param patterns:
:return: list
"""
ls = re.findall(r'[A-Z][a-z]*\S', textSource)
return ls
@staticmethod
def strSplitLow(textSource: str, patterns: str) -> list:
"""
分割字符串 第一个字母小写或大写
:param textSource:
:param patterns:
:return: list
"""
ls = re.findall(r'[A-Z][a-z]*|[a-z]*\S', textSource)
return ls
@staticmethod
def strSplit(textSource: str, patterns: str)->list:
"""
分割字符串
:param textSource:
:param patterns:
:return: list
"""
ls = re.split(r'[' + patterns + '\s]', textSource)
return ls
@staticmethod
def strFunSplit(textSource: str)->list:
"""
分割字符串 用字符串函数
:return: list
"""
ls = textSource.split(" ")
return ls
@staticmethod
def strRegSplit(textSource: str)->list:
"""
分割字符串
:param textSource:
:param patterns:
:return: list
"""
ls = re.findall(r"\S+", textSource)
return ls
@staticmethod
def getdit(textSrource : str) -> list:
"""
提取数字数据 静态方法
所有数值
数字提取:可以用正则表达式来提取数字,包括整数、浮点数等。
"去商店买了8个苹果, 12斤香蕉, 共计12.85元."
:return:
"""
pattern = r'\d+\.\d+|\d+'
match = re.findall(pattern, textSrource)
if match:
print(match) # ['8', '12', '12.85']
else:
print("未找到数值")
return match
@staticmethod
def getint(textSource:str) ->list:
"""
提取整数
:return:
"""
# 匹配浮点数的正则表达式
pattern = r'\d+'
match = re.findall(pattern, textSource)
if match:
print(match)
else:
print("未找到数值")
return match
@staticmethod
def getfloat(textSource:str) ->list:
"""
提取浮点数
:return:
"""
# 匹配浮点数的正则表达式
pattern = r"\d+\.\d+"
match = re.search(pattern, textSource)
if match:
float_number = float(match.group())
print(float_number) #
else:
print("未找到数值")
return match
@staticmethod
def getDate(textSource:str)->list:
"""
提取日期
处理逻辑:
年 4位有效数值 \d{4}
月 0-12 \d{1,2}
日 0-31 \d{1,2}
"""
dateText = ""
if isinstance(textSource, str):
regexRule = r"(\d{4}-\d{1,2}-\d{1,2})"
regexPattern = re.compile(regexRule)
dateList = regexPattern.findall(textSource)
if dateList:
dateText = dateList[0]
return dateText
@staticmethod
def getTime(textSource:str)->list:
"""
提取时间
:param textSource:
:return:
"""
regexRule = r'(0?[0-9]|1[0-9]|2[0-3]):([0-5][0-9]|0?[0-9]):([1-5][0-9]|0?[0-9])'
regexPattern = re.compile(regexRule)
retList = regexPattern.findall(textSource)
if retList:
return retList
else:
print('没有匹配成功.')
return None
@staticmethod
def getUrl(textSource:str)->list:
"""
提取网址
:param textSource:
:return:
"""
# 定义一个正则表达式模式来匹配URL
pattern = r'https?://(?:[-\w.]|(?:%[\da-fA-F]{2}))+'
# 使用re.findall()函数找到所有匹配的URL
urls = re.findall(pattern, textSource)
return urls
@staticmethod
def getMainIs(textSource:str)->bool:
"""
是否有效的邮件
:param textSource:
:return:
"""
pattern = r'^[\w\.-]+@[\w\.-]+\.\w+$'
if re.match(pattern, textSource):
print("有效的邮件地址")
return True
else:
print("无效的邮件地址")
return False
@staticmethod
def getIPIs(textSource:str)->bool:
"""
是否有效的IP
:param textSource:
:return:
"""
#定义IPv4地址的正则表达式
ipv4Pattern = r'^((25[0-5]|2[0-4]\d|[01]?\d{1,2})\.){3}(25[0-5]|2[0-4]\d|[01]?\d{1,2})$'
# 定义 IPv6 地址的正则表达式
ipv6Pattern = r'^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$'
if re.match(ipv4Pattern, textSource):
print("IPv4 地址")
return True
elif re.match(ipv6Pattern, textSource):
print("IPv6 地址")
return True
else:
print("无效的 IP 地址")
return False
@staticmethod
def getChinaMobileIs(textSource:str)->bool:
"""
是否有效的国内手机号码
:param textSource:
:return:
"""
# 匹配以1开头,第二位是3、4、5、6、7、8或9,后面有9位数字的手机号码。
pattern = r'^1[3456789]\d{9}$'
for number in textSource:
if re.match(pattern, number):
print(f'{number} 是有效的手机号码')
return True
else:
print(f'{number} 不是有效的手机号码')
return False
@staticmethod
def getPostCodeIs(textSource:str)->bool:
"""
:param textSource:
:return:
"""
pattern = r'^\d{6}$' # 匹配6位数字
if re.match(pattern, textSource):
print("邮政编码有效!")
return True
else:
print("邮政编码无效!")
return False
@staticmethod
def getICDIs(textSource:str)->bool:
pattern = r'^[1-9]\d{5}(18|19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[1-2]\d|3[0-1])\d{3}[0-9Xx]$'
match = re.match(pattern, textSource)
if match:
print("身份证号码合法!")
return True
else:
print("身份证号码不合法!")
return False
@staticmethod
def extractHtmltags(textSource:str)->list:
"""
:param textSource:
:return:
"""
pattern = r"<([^>]+)>"
tags = re.findall(pattern, textSource)
return tags
@staticmethod
def getStock(textSource:str)->list:
"""
:param textSource:
:return:
"""
# 提取公司简称
companyNamePattern = r"[\u4e00-\u9fff]+"
companyNameMatches = re.findall(companyNamePattern, textSource)
companyCame = companyNameMatches if companyNameMatches else None
# 提取证券代码 6位数
stockCodePattern = r"\d{6}"
stockCodeMatches = re.findall(stockCodePattern, textSource)
stockCode = stockCodeMatches if stockCodeMatches else None
print("公司简称:", companyCame)
print("证券代码:", stockCode)
return companyCame,stockCode
字符 |
含义 |
举例 |
备注 |
符合条件 |
. |
一个任意字符 |
a..b |
a开头b结尾,中间两个任意字符 |
a|2b |
\w |
一个字母/数字/下划线 |
\w... |
字母/数字/下划线开头 |
o8js |
\W |
非字母/数字/下划线 |
\Wabc |
#abc | |
\s |
一个空白字符 |
a\sb |
a\nb | |
\S |
一个非空白字符 |
\S… |
三个数字 |
2jkh |
\d |
数字字符 |
\d\d\d |
675 | |
\D |
非数字字符 |
\D\w\w\w |
#h7_ | |
[] |
括号中任意一个字符[1-9]数字1到9 [a-z]小写 [A-Z]大写 |
[abc]aaa |
a/b/c开头 |
caaa |
[^字符集] |
一个不在字符集中的任意字符 |
[^abc]... |
非a/b/c开头 |
898i |
^ |
字符串开头 |
^\ddid |
866 | |
$ |
字符串结尾 |
abc$ |
abc | |
\b |
(检测边界) |
Abc\b\saaa |
abclb\saaa |
abc aaa |
* |
匹配≥0次 |
\d* |
数字0或很多次 1个或很多个数字开 |
12312 |
+ |
匹配≥1次 |
\d+abc |
1个或很多个数字开头 |
99abc |
? |
匹配0/1次 |
a?123 |
有a或者无a |
a123 |
{N} |
匹配N次 | |||
{M,N} |
匹配M到N次 | |||
{M,} |
至少匹配M次 | |||
{,N} |
最多匹配N次 | |||
https://docs.python.org/zh-cn/3.11//howto/regex.htmll
match()和search()都只匹配出一个符合条件的字符串,若想要所有,可以使用re.findall()
语法 |
释义 |
| |
或者 |
() |
|
\ |
转义符号,在特殊的符号前加\,来让特殊的符号没有意义不管在哪儿都需要转义 -在口外面没有特殊功能,在口中要表示-本身,就不要放在两个字符之间()需要转义 |
compile |
将正则表达式字符串转换成正则表达式对象 |
fullmatch/match |
匹配对象或者None |
string |
获取被匹配的原字符串 |
findall |
获取字符串中满足正则表达式的所有的子串,返回一个列表 |
finditer |
查找所有满足正则条件的子串,返回值是迭代器,迭代器中的元素是匹配对象 |
split |
将字符串按照满足正则表达式条件的子串进行分割 |
sub(正则,repl,字符串) |
字符串中满足正则表达式条件的子串替换成repl。返回替换后的字符串 |