re匹配中文:[u4e00-u9fa5]
验证一个数字: ^\d$
必须有一个数字,最少一位: ^\d+$
只能出现数字,且位数为5-10位: ^\d{5,10}$
注册者年龄 16岁以上,99岁以下: ^[16-99]$
只能输入英文字符和数字: ^[A-Za-z0-9]$
验证qq号码: ^[0-9]{5,12}
import re
p = re.compile(r'\d+')
## 3,10 表示查找的范围
m = p.match("qwq1233qewsfsfrrgrsc",3,10)
print(m)
## match 可以输入参数;查到的结果只包含一个 表示第一次匹配成功的内容
-------------------------------------------------------------
结果:<_sre.SRE_Match object; span=(3, 7), match='1233'>
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
import re
# I表示忽略大小写
p = re.compile(r'([a-z]+) ([a-z]+)',re.I)
m = p.match('I am really love you')
print(m)
-----------------------------------------------------------------
结果:<_sre.SRE_Match object; span=(0, 4), match='I am'>
import re
p = re.compile(r'\d+')
m = p.findall("one1two2three34")
a = p.search("one1two2three34")
print(a)
print(m)
---------------------------------------------------------------------------
结果:<_sre.SRE_Match object; span=(3, 4), match='1'>
['1', '2', '34']
+++++++++++++++++++++++++++++++++++++++++++++++++++++
#替换
import re
p = re.compile(r'(\w+) (\w+)')
s = "hello 123 wang 123 xiao 456 jing, i love you"
rst = p.sub(r'hello world',s)
print(rst)
---------------------------------------
结果:hello world hello world hello world jing, hello world you
import re
title = 'nameage'
p1 = re.compile(r".*")
p2 = re.compile(r'.*?')
r1 = p1.match(title)
r2 = p2.search(title)
print(r1.group())
print(r2.group())
-----------------------------------------
结果:nameage
name
import re
title = u'世界你好 hello moto'
p = re.compile(r'[\u4e00-\u9fa5]+')
rst = p.findall(title)
print(rst)
-----------------------------
结果:['世界你好']
在xml文件中查找信息的一套规则/语言 根据xml元素
开源的
nodename: 选取此节点的所有子节点
/: 从根节点开始选取
//: 选取节点,不考虑位置
. :选取当前节点
… : 选取当前节点的父亲
@: 选取属性 //age[@detail] 或者 //div[@class=‘title’]
xpath中查找一般按照路径方法查找
/School/Student[1]
/School/Student[last()]
/School/Student[last()-1]
/School/Student[position()❤️] 前两个
//Student[@score]
//Student[@score=‘99’]
//STudent[@score]/Age