首页 > 其他 > 详细

10.正则表达式

时间:2018-04-20 23:08:39      阅读:151      评论:0      收藏:0      [点我收藏+]
# 1.检查一串数字是否是电话号码
# 2.检查一个字符串是否符合email
# 3.把一个文本里指定的单词替换为另一个单词
import re
# a = C|C++|Java|C#|Python|Javascript
# print(a.index(Python) > -1)  # True
# print(Python in a)           # True

# r=re.findall(Python,a)
# print(r)      # [Python]
# if len(r) > 0:
#     print(字符串中包含Python)
# else:
#     print(No)


# b = C0C++7Java8C#9Python6Javascript
# 查找所有的数字
# r=re.findall(\d,b)
# print(r)      # [0, 7, 8, 9, 6]
# 查找所有的非数字
# r=re.findall(\D,b)
# print(r)      # [C, C, +, +, J, a, v, a, C, #, P, y, t, h, o, n, J, a, v, a, s, c, r, i, p, t]


c = abc, acc, adc, aec, afc, ahc
# 查找中间一个字母是c或f的单词
# r=re.findall(a[cf]c,c)
# print(r)      # [acc, afc]
# 查找中间一个字母不是c并且也不是f的单词
# r=re.findall(a[^cf]c,c)
# print(r)      # [abc, adc, aec, ahc]
# 查找中间一个字母在c-f的单词
# r=re.findall(a[c-f]c,c)
# print(r)      # [acc, adc, aec, afc]


# 概括字符集
# \d \D
# \w 单词字符 \W
# \s 空白字符 \S
# .  匹配除换行符\n之外的所有字符
# e = python1111 java&6\t7\r8\n__php
# r=re.findall([0-9],e) 
# print(r)  # [1, 1, 1, 1, 6, 7, 8]
# r=re.findall([^0-9],e)
# print(r)  # [p, y, t, h, o, n,  , j, a, v, a, &, \t, \r, \n, _, _, p, h, p]
# r=re.findall(\w,e)  # \w等价于[A-Za-z0-9_]
# print(r)  # [p, y, t, h, o, n, 1, 1, 1, 1, j, a, v, a, 6, 7, 8, _, _, p, h, p]
# r=re.findall(\W,e)
# print(r)  # [ , &, \t, \r, \n]
# r=re.findall(\s,e)
# print(r)  # [ , \t, \r, \n]

# 数量词 
# * 匹配0次或无限多次
# + 匹配1次或无限多次
# ? 匹配0次或1次
# 贪婪匹配
# r=re.findall([a-z]{3,6},e)
# print(r)  # [python, java, php]
# 非贪婪匹配
# r=re.findall([a-z]{3,6}?,e)
# print(r)  # [pyt, hon, jav, php]

# f = pytho0python1pythonn2
# r=re.findall(python*,f)
# print(r)  # [pytho, python, pythonn]
# r=re.findall(python+,f)
# print(r)  # [python, pythonn]
# r=re.findall(python?,f)
# print(r)  # [pytho, python, python]

# 贪婪模式
# r=re.findall(python{1,2},f)
# print(r)  # [python, pythonn]
# 非贪婪模式
# r=re.findall(python{1,2}?,f)
# print(r)  # [python, python]


# 边界匹配
# qq = 100001
# 4~8位
# r=re.findall(\d{4,8},qq)
# print(r)  # [100001]
# qq2 = 100000001
# r=re.findall(^\d{4,8}$,qq2)
# print(r)  # []
# r=re.findall(000,qq2)
# print(r)  # [000, 000]
# r=re.findall(^000,qq2)
# print(r)  # []
# r=re.findall(000$,qq2)
# print(r)  # []

# 组 ()
# g = pythonpythonpythonpythonpythonJS
# r=re.findall((python){3}(JS),g)
# print(r)  # [python]

# lanage = PythonC#\nJavaPHP
# 忽略字母大小写匹配
# r=re.findall(c#,lanage,re.I)
# print(r)  # [C#]
# r=re.findall(c#.{1},lanage,re.I | re.S)
# print(r)  # [C#\n]

# 字符串替换
# lanage2 = PythonC#JavaC#PHPC#
# r = re.sub(C#,Go,lanage2,1)
# print(r)  # PythonGoJavaC#PHPC#
# r = re.sub(C#,Go,lanage2,0)
# print(r)  # PythonGoJavaGoPHPGo

# lanage = lanage2.replace(C#,Go)
# print(lanage)  # PythonGoJavaGoPHPGo


# def convert(value):
#     # print(value)  # <_sre.SRE_Match object; span=(6, 8), match=C#>
#                     # <_sre.SRE_Match object; span=(12, 14), match=C#>
#                     # <_sre.SRE_Match object; span=(17, 19), match=C#>
#     matched = value.group()
#     return !! + matched + !!

# r = re.sub(C#,convert,lanage2)
# print(r)  # Python!!C#!!Java!!C#!!PHP!!C#!!


s = A8C3721D86
# def convert(value):
#     matched = value.group()
#     if int(matched) >= 6:
#         return 9
#     else:
#         return 0

# r = re.sub(\d,convert,s)
# print(r)  # A9C0900D99

# 从字符串首字母开始匹配
# r = re.match(\d,s)
# print(r)  # None

# 搜索整个字符串直,返回第一次找到的字符串
# r = re.search(\d,s)
# print(r)   # <_sre.SRE_Match object; span=(1, 2), match=8>
# print(r.group())  # 8

# # 返回第一次查找到字符串的位置
# print(r.span())   # (1, 2)


# j = life is short,i use python
# r = re.search(life(.*)python,j)
# print(r.group(0))   # life is short,i use python
# print(r.group(1))   #  is short,i use 

# r = re.findall(life(.*)python,j)
# print(r)   # [ is short,i use ]

h = life is short,i use python,i love python
r = re.search(life(.*)python(.*)python,h)
# print(r.group(0))     # life is short,i use python,i love python
# print(r.group(1))     #  is short,i use
# print(r.group(2))     # ,i love
# print(r.group(0,1,2)) # (life is short,i use python,i love python,  is short,i use , ,i love )
# print(r.groups())     # ( is short,i use , ,i love )

 

10.正则表达式

原文:https://www.cnblogs.com/zouke1220/p/8893853.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!