美文网首页
正则表达式

正则表达式

作者: 是我真的是我 | 来源:发表于2019-10-16 15:58 被阅读0次
  • 使用演示
  • 模式对象

使用演示

import re

print(re.search(r'com', 'http://test.com'))
# <re.Match object; span=(12, 15), match='com'>
print('http://test.com'.find('com'))
# 12

print(re.search(r'.', 'http://test.com'))
# <re.Match object; span=(0, 1), match='h'>
print(re.search(r'test.', 'http://test.com'))
# <re.Match object; span=(7, 12), match='test.'>
print(re.search(r'\.', 'http://test.com'))
# <re.Match object; span=(11, 12), match='.'>

print(re.search(r'\d', 'http://123test.com'))
# <re.Match object; span=(7, 8), match='1'>
print(re.search(r'\d\d', 'http://123test.com'))
# <re.Match object; span=(7, 9), match='12'>

print(re.search(r'[abc]', 'Aab'))
# <re.Match object; span=(1, 2), match='a'>
print(re.search(r'[a-z]', 'Aab'))
# <re.Match object; span=(1, 2), match='a'>
print(re.search(r'ab{3,5}c', 'abbbbbc'))
# <re.Match object; span=(0, 7), match='abbbbbc'>

import re

'逻辑或'
print(re.search(r'python(2|3)', 'python3'))
# <re.Match object; span=(0, 7), match='python1'>

'以...开始;\A同功能,但是限制更强,其他参数限制不了'
print(re.search(r'^python', 'python3'))
# <re.Match object; span=(0, 6), match='python'>
print(re.search(r'^python', 'this_python3'))
# None

'以...结尾;\Z同功能,但是限制更强,其他参数限制不了'
print(re.search(r'python$', 'python3'))
# None
print(re.search(r'python$', 'python'))
# <re.Match object; span=(0, 6), match='python'>

'"\" + 数字 表示要匹配的组个数;当数字是0开头或三位则表示八进制'
print(re.search(r'(python)\1', 'python'))
# None
print(re.search(r'(python)\1', 'pythonpython'))
# <re.Match object; span=(0, 12), match='pythonpython'>

print(re.search(r'.', 'python'))
# <re.Match object; span=(0, 1), match='p'>
print(re.search(r'\.', 'python'))
# None
print(re.search(r'[.]', 'python.py'))
# <re.Match object; span=(6, 7), match='.'>
print(re.search(r'[\n]', 'python.\ny'))
# <re.Match object; span=(7, 8), match='\n'>

print(re.findall(r'[a-z]', 'abcABCabc'))
# ['a', 'b', 'c', 'a', 'b', 'c']
print(re.findall(r'[^a-z]', 'abcABCabc'))
# ['A', 'B', 'C']
print(re.findall(r'^[a-z]', 'abcABCabc'))
# ['a']

'''
* 零次或多次
+ 一次或多次
? 零次或一次
'''

s = '<html><div></div></html>'
'贪婪模式'
print(re.search(r'<.+>', s))
# <re.Match object; span=(0, 24), match='<html><div></div></html>'>
'非贪婪模式'
print(re.search(r'<.+?>', s))
# <re.Match object; span=(0, 6), match='<html>'>

'''
\d 匹配数字0-9
\s 匹配空白字符(包括\t\n\r\f\v)
\w 匹配a-zA-Z0-9以及语言文字和下横线
'''
print(re.findall(r'\w', '你好_(python!)'))
# ['你', '好', '_', 'p', 'y', 't', 'h', 'o', 'n']


import re

'分组'
res = re.search(r'(\w+) (\w+)', 'hi python, haha')
print(res)
# <re.Match object; span=(0, 9), match='hi python'>
print(res.group())
# hi python
print(res.group(1))
# hi
print(res.group(2))
# python

print(res.start())
# 0
print(res.end())
# 9
print(res.span())
# (0, 9)

模式对象

p = re.compile(r'[A-Z]')
print(p.search('Learn Python!'))
# <re.Match object; span=(0, 1), match='L'>
print(p.findall('Learn Python!'))
# ['L', 'P']

相关文章

  • Linux命令行与Shell脚本编程大全-shell正则表达式

    本章内容: 定义正则表达式 了解基本正则表达式 扩展正则表达式 创建正则表达式 定义正则表达式 正则表达式是你定义...

  • 正则相关

    正则表达式基本语法 正则表达式常见字符 正则表达式特殊字符 正则表达式数量词 正则表达式边界匹配 正则表达式逻辑或...

  • 正则表达式系列-1

    正则表达式系列-1正则表达式系列-2正则表达式系列-3正则表达式系列-4 什么是正则表达式 正则表达式就是用事先定...

  • 正则表达式

    正则表达式 - 教程正则表达式 - 简介正则表达式 - 语法正则表达式 - 元字符正则表达式 - 运算符优先级正则...

  • Python基础入门 - 正则表达式与综合实战

    1. 初识正则表达式 1.1 介绍 步骤介绍正则表达式入门及应用正则表达式的进阶正则表达式案例 1.2 正则表达式...

  • Java正则表达式参考

    Java正则表达式入门 java正则表达式应用 深入浅出之正则表达式(一) 深入浅出之正则表达式(二) 正则表达式...

  • 正则表达式

    正则表达式 正则表达式就是记录文本规则的代码 正则表达式常用的元字符 正则表达式常用的限定符 正则表达式举例:这里...

  • Python爬虫(十)_正则表达式

    本篇将介绍python正则表达式,更多内容请参考:【python正则表达式】 什么是正则表达式 正则表达式,又称规...

  • python正则表达式

    本篇将介绍python正则表达式,更多内容请参考:【python正则表达式】 什么是正则表达式 正则表达式,又称规...

  • 正则表达式

    了解正则表达式基本语法 能够使用JavaScript的正则对象 正则表达式简介 什么是正则表达式 正则表达式:用于...

网友评论

      本文标题:正则表达式

      本文链接:https://www.haomeiwen.com/subject/jccumctx.html