iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Python的RegEx正则表达式怎么使用
  • 100
分享到

Python的RegEx正则表达式怎么使用

Pythonregex 2023-05-19 20:05:40 100人浏览 泡泡鱼

Python 官方文档:入门教程 => 点击学习

摘要

RegEx 或正则表达式是形成搜索模式的字符序列。RegEx 可用于检查字符串是否包含指定的搜索模式。RegEx 模块python 提供名为 re 的内置包,可用于处理正则表达式。导入 re 模块:import rePython 中的 Re

RegEx 或正则表达式是形成搜索模式的字符序列。

RegEx 可用于检查字符串是否包含指定的搜索模式。

RegEx 模块

python 提供名为 re 的内置包,可用于处理正则表达式。

导入 re 模块:

import re

Python 中的 RegEx

导入 re 模块后,就可以开始使用正则表达式了:

实例

检索字符串以查看它是否以 “China” 开头并以 “country” 结尾:

import re

txt = "China is a great country"
x = re.search("^China.*country$", txt)

运行实例

import re

txt = "China is a great country"
x = re.search("^China.*country$", txt)

if (x):
  print("YES! We have a match!")
else:
  print("No match")

Python的RegEx正则表达式怎么使用

RegEx 函数

re 模块提供了一组函数,允许我们检索字符串以进行匹配:

Python的RegEx正则表达式怎么使用

元字符

元字符是具有特殊含义的字符

字符:[] 描述:一组字符 示例:“[a-m]”

import re

str = "The rain in Spain"

#Find all lower case characters alphabetically between "a" and "m":

x = re.findall("[a-m]", str)
print(x)

运行示例

Python的RegEx正则表达式怎么使用

字符: 描述:示意特殊序列(也可用于转义特殊字符) 示例:“\d”

import re

str = "That will be 59 dollars"

#Find all digit characters:

x = re.findall("\d", str)
print(x)

运行示例

Python的RegEx正则表达式怎么使用

字符:. 描述:任何字符(换行符除外) 示例: “he…o”

import re

str = "hello world"

#Search for a sequence that starts with "he", followed by two (any) characters, and an "o":

x = re.findall("he..o", str)
print(x)

运行示例

Python的RegEx正则表达式怎么使用

字符:^ 描述:起始于 示例: “^hello”

import re

str = "hello world"

#Check if the string starts with 'hello':

x = re.findall("^hello", str)
if (x):
  print("Yes, the string starts with 'hello'")
else:
  print("No match")

运行示例

Python的RegEx正则表达式怎么使用

字符:$ 描述:结束于 示例:“world$”

import re

str = "hello world"

#Check if the string ends with 'world':

x = re.findall("world$", str)
if (x):
  print("Yes, the string ends with 'world'")
else:
  print("No match")

运行示例

Python的RegEx正则表达式怎么使用

字符:* 描述:零次或多次出现 示例:“aix*”

import re

str = "The rain in Spain falls mainly in the plain!"

#Check if the string contains "ai" followed by 0 or more "x" characters:

x = re.findall("aix*", str)

print(x)

if (x):
  print("Yes, there is at least one match!")
else:
  print("No match")

运行示例

Python的RegEx正则表达式怎么使用

字符:+ 描述:一次或多次出现 示例: “aix+”

import re

str = "The rain in Spain falls mainly in the plain!"

#Check if the string contains "ai" followed by 1 or more "x" characters:

x = re.findall("aix+", str)

print(x)

if (x):
  print("Yes, there is at least one match!")
else:
  print("No match")

运行示例

Python的RegEx正则表达式怎么使用

字符:{} 描述: 确切地指定的出现次数 示例:“al{2}”

import re

str = "The rain in Spain falls mainly in the plain!"

#Check if the string contains "a" followed by exactly two "l" characters:

x = re.findall("al{2}", str)

print(x)

if (x):
  print("Yes, there is at least one match!")
else:
  print("No match")

运行示例

Python的RegEx正则表达式怎么使用

字符:| 描述:两者任一 示例:“falls|stays”

import re

str = "The rain in Spain falls mainly in the plain!"

#Check if the string contains either "falls" or "stays":

x = re.findall("falls|stays", str)

print(x)

if (x):
  print("Yes, there is at least one match!")
else:
  print("No match")

运行示例

Python的RegEx正则表达式怎么使用

字符:() 描述:捕获和分组

特殊序列

特殊序列指的是 \ 后跟下表中的某个字符,拥有特殊含义。

字符:\A 描述:如果指定的字符位于字符串的开头,则返回匹配项 示例:“\AThe”

import re

str = "The rain in Spain"

#Check if the string starts with "The":

x = re.findall("\AThe", str)

print(x)

if (x):
  print("Yes, there is a match!")
else:
  print("No match")

运行示例

Python的RegEx正则表达式怎么使用

字符:\b

描述:返回指定字符位于单词的开头或末尾的匹配项

示例:r"\bain"

import re

str = "The rain in Spain"

#Check if "ain" is present at the beginning of a Word:

x = re.findall(r"\bain", str)

print(x)

if (x):
  print("Yes, there is at least one match!")
else:
  print("No match")

运行示例

Python的RegEx正则表达式怎么使用

示例:r"ain\b"

import re

str = "The rain in Spain"

#Check if "ain" is present at the end of a WORD:

x = re.findall(r"ain\b", str)

print(x)

if (x):
  print("Yes, there is at least one match!")
else:
  print("No match")

运行示例

Python的RegEx正则表达式怎么使用

字符:\B

描述:返回指定字符存在的匹配项,但不在单词的开头(或结尾处)

示例:r"\Bain"

import re

str = "The rain in Spain"

#Check if "ain" is present, but NOT at the beginning of a word:

x = re.findall(r"\Bain", str)

print(x)

if (x):
  print("Yes, there is at least one match!")
else:
  print("No match")

运行示例

Python的RegEx正则表达式怎么使用

示例:r"ain\B"

import re

str = "The rain in Spain"

#Check if "ain" is present, but NOT at the end of a word:

x = re.findall(r"ain\B", str)

print(x)

if (x):
  print("Yes, there is at least one match!")
else:
  print("No match")

运行示例

Python的RegEx正则表达式怎么使用

字符:\d

描述:返回字符串包含数字的匹配项(数字 0-9)

示例:“\d”

import re

str = "The rain in Spain"

#Check if the string contains any digits (numbers from 0-9):

x = re.findall("\d", str)

print(x)

if (x):
  print("Yes, there is at least one match!")
else:
  print("No match")

运行示例

Python的RegEx正则表达式怎么使用

字符:\D

描述:返回字符串不包含数字的匹配项

示例:“\D”

import re

str = "The rain in Spain"

#Return a match at every no-digit character:

x = re.findall("\D", str)

print(x)

if (x):
  print("Yes, there is at least one match!")
else:
  print("No match")

运行示例

Python的RegEx正则表达式怎么使用

字符:\s

描述:返回字符串包含空白字符的匹配项

示例:“\s”

import re

str = "The rain in Spain"

#Return a match at every white-space character:

x = re.findall("\s", str)

print(x)

if (x):
  print("Yes, there is at least one match!")
else:
  print("No match")

运行示例

Python的RegEx正则表达式怎么使用

字符:\S

描述:返回字符串不包含空白字符的匹配项

示例:“\S”

import re

str = "The rain in Spain"

#Return a match at every NON white-space character:

x = re.findall("\S", str)

print(x)

if (x):
  print("Yes, there is at least one match!")
else:
  print("No match")

运行示例

Python的RegEx正则表达式怎么使用

字符:\w

描述: 返回一个匹配项,其中字符串包含任何单词字符 (从 a 到 Z 的字符,从 0 到 9 的数字和下划线 _ 字符)

示例:“\w”

import re

str = "The rain in Spain"

#Return a match at every word character (characters from a to Z, digits from 0-9, and the underscore _ character):

x = re.findall("\w", str)

print(x)

if (x):
  print("Yes, there is at least one match!")
else:
  print("No match")

运行示例

Python的RegEx正则表达式怎么使用

字符:\W

描述:返回一个匹配项,其中字符串不包含任何单词字符

示例:“\W”

import re

str = "The rain in Spain"

#Return a match at every NON word character (characters NOT between a and Z. Like "!", "?" white-space etc.):

x = re.findall("\W", str)

print(x)

if (x):
  print("Yes, there is at least one match!")
else:
  print("No match")

运行示例

Python的RegEx正则表达式怎么使用

字符:\Z

描述:如果指定的字符位于字符串的末尾,则返回匹配项 。

示例:“Spain\Z”

import re

str = "The rain in Spain"

#Check if the string ends with "Spain":

x = re.findall("Spain\Z", str)

print(x)

if (x):
  print("Yes, there is a match!")
else:
  print("No match")

运行示例

Python的RegEx正则表达式怎么使用

集合(Set)

集合(Set)是一对方括号 [] 内的一组字符,具有特殊含义。

字符:[arn]

描述:返回一个匹配项,其中存在指定字符(a,r 或 n)之一

示例

import re

str = "The rain in Spain"

#Check if the string has any a, r, or n characters:

x = re.findall("[arn]", str)

print(x)

if (x):
  print("Yes, there is at least one match!")
else:
  print("No match")

运行示例

Python的RegEx正则表达式怎么使用

字符:[a-n]

描述:返回字母顺序 a 和 n 之间的任意小写字符匹配项

示例

import re

str = "The rain in Spain"

#Check if the string has any characters between a and n:

x = re.findall("[a-n]", str)

print(x)

if (x):
  print("Yes, there is at least one match!")
else:
  print("No match")

运行示例

Python的RegEx正则表达式怎么使用

字符:[^arn]

描述:返回除 a、r 和 n 之外的任意字符的匹配项

示例

import re

str = "The rain in Spain"

#Check if the string has other characters than a, r, or n:

x = re.findall("[^arn]", str)

print(x)

if (x):
  print("Yes, there is at least one match!")
else:
  print("No match")

运行示例

Python的RegEx正则表达式怎么使用

字符:[0123]

描述:返回存在任何指定数字(0、1、2 或 3)的匹配项

示例

import re

str = "The rain in Spain"

#Check if the string has any 0, 1, 2, or 3 digits:

x = re.findall("[0123]", str)

print(x)

if (x):
  print("Yes, there is at least one match!")
else:
  print("No match")

运行示例

Python的RegEx正则表达式怎么使用

字符:[0-9]

描述:返回 0 与 9 之间任意数字的匹配

示例

import re

str = "8 times before 11:45 AM"

#Check if the string has any digits:

x = re.findall("[0-9]", str)

print(x)

if (x):
  print("Yes, there is at least one match!")
else:
  print("No match")

运行示例

Python的RegEx正则表达式怎么使用

字符:[0-5][0-9]

描述:返回介于 0 到 9 之间的任何数字的匹配项

示例

import re

str = "8 times before 11:45 AM"

#Check if the string has any two-digit numbers, from 00 to 59:

x = re.findall("[0-5][0-9]", str)

print(x)

if (x):
  print("Yes, there is at least one match!")
else:
  print("No match")

运行示例

Python的RegEx正则表达式怎么使用

字符:[a-zA-Z]

描述:返回字母顺序 a 和 z 之间的任何字符的匹配,小写或大写

示例

import re

str = "8 times before 11:45 AM"

#Check if the string has any characters from a to z lower case, and A to Z upper case:

x = re.findall("[a-zA-Z]", str)

print(x)

if (x):
  print("Yes, there is at least one match!")
else:
  print("No match")

运行示例

Python的RegEx正则表达式怎么使用

字符:[+]

描述:在集合中,+、*、.、|、()、$、{} 没有特殊含义,因此 [+] 表示:返回字符串中任何 + 字符的匹配项。

示例

import re

str = "8 times before 11:45 AM"

#Check if the string has any + characters:

x = re.findall("[+]", str)

print(x)

if (x):
  print("Yes, there is at least one match!")
else:
  print("No match")

运行示例

Python的RegEx正则表达式怎么使用

findall() 函数

findall() 函数返回包含所有匹配项的列表。

实例

打印所有匹配的列表

import re

str = "China is a great country"
x = re.findall("a", str)
print(x)

运行实例

Python的RegEx正则表达式怎么使用

这个列表以被找到的顺序包含匹配项。

如果未找到匹配项,则返回空列表。

实例

如果未找到匹配,则返回空列表:

import re

str = "China is a great country"
x = re.findall("USA", str)
print(x)

运行实例

Python的RegEx正则表达式怎么使用

search() 函数

search() 函数搜索字符串中的匹配项,如果存在匹配则返回 Match 对象。

如果有多个匹配,则仅返回首个匹配项。

实例

在字符串中搜索第一个空白字符

import re

str = "China is a great country"
x = re.search("\s", str)

print("The first white-space character is located in position:", x.start())

运行实例

Python的RegEx正则表达式怎么使用

如果未找到匹配,则返回值 None:

实例

进行不返回匹配的检索

import re

str = "China is a great country"
x = re.search("USA", str)
print(x)

运行实例

Python的RegEx正则表达式怎么使用

split() 函数

split() 函数返回一个列表,其中字符串在每次匹配时被拆分。

实例

在每个空白字符处进行拆分

import re

str = "China is a great country"
x = re.split("\s", str)
print(x)

运行实例

Python的RegEx正则表达式怎么使用

可以通过指定 maxsplit 参数来控制出现次数:

实例

仅在首次出现时拆分字符串:

import re

str = "China is a great country"
x = re.split("\s", str, 1)
print(x)

运行实例

Python的RegEx正则表达式怎么使用

sub() 函数

sub() 函数把匹配替换为您选择的文本

实例

用数字 9 替换每个空白字符

import re

str = "China is a great country"
x = re.sub("\s", "9", str)
print(x)

运行实例

Python的RegEx正则表达式怎么使用

可以通过指定 count 参数来控制替换次数:

实例

替换前两次出现

import re

str = "China is a great country"
x = re.sub("\s", "9", str, 2)
print(x)

运行实例

Python的RegEx正则表达式怎么使用

Match 对象

Match 对象是包含有关搜索和结果信息的对象。

注释:如果没有匹配,则返回值 None,而不是 Match 对象。

实例

执行会返回 Match 对象的搜索:

import re

str = "China is a great country"
x = re.search("a", str)
print(x) # 将打印一个对象

运行实例

Python的RegEx正则表达式怎么使用

Match 对象提供了用于取回有关搜索及结果信息的属性和方法:

  • span() 返回的元组包含了匹配的开始和结束位置

  • .string 返回传入函数的字符串

  • group() 返回匹配的字符串部分

实例

打印首个匹配出现的位置(开始和结束位置)。

正则表达式查找以大写 “C” 开头的任何单词:

import re

str = "China is a great country"
x = re.search(r"\bC\w+", str)
print(x.span())

运行实例

Python的RegEx正则表达式怎么使用

实例

打印传入函数的字符串

import re

str = "China is a great country"
x = re.search(r"\bC\w+", str)
print(x.string)

运行实例

Python的RegEx正则表达式怎么使用

实例

打印匹配的字符串部分

正则表达式查找以大写 “C” 开头的任何单词:

import re

str = "China is a great country"
x = re.search(r"\bC\w+", str)
print(x.group())

运行实例

Python的RegEx正则表达式怎么使用

注释:如果没有匹配项,则返回值 None,而不是 Match 对象。

以上就是Python的RegEx正则表达式怎么使用的详细内容,更多请关注编程网其它相关文章!

--结束END--

本文标题: Python的RegEx正则表达式怎么使用

本文链接: https://www.lsjlt.com/news/213694.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Python的RegEx正则表达式怎么使用
    RegEx 或正则表达式是形成搜索模式的字符序列。RegEx 可用于检查字符串是否包含指定的搜索模式。RegEx 模块Python 提供名为 re 的内置包,可用于处理正则表达式。导入 re 模块:import rePython 中的 Re...
    99+
    2023-05-19
    Python regex
  • C#正则表达式Regex类怎么使用
    这篇文章主要介绍“C#正则表达式Regex类怎么使用”,在日常操作中,相信很多人在C#正则表达式Regex类怎么使用问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”C#正则表达式Regex类怎么使用”的疑惑有所...
    99+
    2023-06-22
  • C#正则表达式Regex类的用法
    一、C#正则表达式符号模式 字  符 描  述 ...
    99+
    2024-04-02
  • C#正则表达式Regex用法详解
    目录一、正则表达式应用举例1、C#校验合法性:2、C#限制输入3、正则表达式匹配闭合HTML标签(支持嵌套)1、分组构造2、举例:二、.Net正则表达式测试器1、功能简介2、下载与安...
    99+
    2024-04-02
  • 如何入门正则表达式Regex
    这篇文章给大家介绍如何入门正则表达式Regex,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。今天要分享的内容是正则表达式Regex。某天中午正要拿起手机打...
    99+
    2024-04-02
  • 在python正则表达式中是怎样正确使用正则表达式
    这篇文章将为大家详细讲解有关在python正则表达式中是怎样正确使用正则表达式,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。现在我们已经看了一些简单的正则表达式,那么我们实际在 Python...
    99+
    2023-06-17
  • python 正则表达式的使用
    目录1、正则表达式 1.1 正则表达式字符串1.1.1 元字符1.1.2 字符转义1.1.3 开始与结束字符1.2 字符类1.2.1 定义字符类1.2.2 字符串取反1.2.3 区间...
    99+
    2024-04-02
  • C#正则表达式(Regex类)用法实例总结
    目录前言1.正则表达式的概念1.1正则表达式的组成1.1.1元字符是什么1.1.2定位元字符1.2三个括号1.3简化正则表达式1.4@符号的作用1.5正则表达式可以实现四种功能:2....
    99+
    2022-11-13
    c#正则表达式的用法 c# 正则式 C# 正则表达式
  • Python入门教程(二十九)Python的RegEx正则表达式
    RegEx 或正则表达式是形成搜索模式的字符序列。 RegEx 可用于检查字符串是否包含指定的搜索模式。 RegEx 模块 Python 提供名为 re 的内置包,可用于处理正则表达...
    99+
    2023-05-18
    Python RegEx Python正则表达式
  • python中的正则表达式怎么使用
    这篇文章主要讲解了“python中的正则表达式怎么使用”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“python中的正则表达式怎么使用”吧!在Python中需要通过正则表达式对字符串进行匹配...
    99+
    2023-07-04
  • Regex正则表达式判断密码强度
    目录需求在线测试Regex正文密码强度分类Regex分析中强度密码同理。高强度密码同理。补充:密码强度 弱 中 强 正则表达式判断需求   最近在最做一个软件的注...
    99+
    2023-02-01
    正则表达式判断密码强度 正则表达式判断密码
  • python的正则表达式怎么用
    这篇文章主要为大家展示了“python的正则表达式怎么用”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“python的正则表达式怎么用”这篇文章吧。一、正则表达式–元字符re 模块使 Python...
    99+
    2023-06-25
  • Python中怎么使用正则表达式及正则表达式匹配规则是什么
    1 导库import re2 使用模板re_pattern = re.compile(pattern, flags=0) result = re.findall(re_pattern,string)3 说明参数描述pattern匹配的正则表...
    99+
    2023-05-14
    Python
  • python 中正则表达式的使用
    正则表达式(re)(Regular Expression)。正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的一些特定字符、及这些特定字符的组合,组成一个“规则字符串”,这个“规则字符串”用来表达对字符串的一种过滤逻辑。在pyth...
    99+
    2023-01-31
    正则表达式 python
  • Python中使用正则表达式及正则表达式匹配规则详解
    目录1 导库2 使用模板3 说明4 示例5 正则表达式匹配规则1 导库 import re 2 使用模板 re_pattern = re.compile(pattern, flags...
    99+
    2023-03-22
    Python正则表达式匹配规则 Python正则表达式
  • Python中的正则表达式怎么用
    这篇文章主要为大家展示了“Python中的正则表达式怎么用”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“Python中的正则表达式怎么用”这篇文章吧。1.正则表达式是什么很简单就是一种字符串匹配...
    99+
    2023-06-25
  • 怎么使用javascript正则表达式
    本篇内容介绍了“怎么使用javascript正则表达式”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!表单验...
    99+
    2024-04-02
  • C++怎么使用正则表达式
    今天小编给大家分享一下C++怎么使用正则表达式的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。目正则表达式正则表达式(regu...
    99+
    2023-06-30
  • js 怎么使用正则表达式
    在 JavaScript 中,可以使用正则表达式的两种方式:使用 RegExp 对象或者使用正则表达式字面量。1. 使用 RegEx...
    99+
    2023-09-20
    js
  • python正则表达式
    笔记:一:简介 正则表达式是一个特殊的字符序列,它能帮助你方便的检查一个字符串是否与某种模式匹配。 主要介绍Python中常用的正则表达式处理函数 提高工作效率,完成内置函数无法完成的任务! 搜索常用正则表达式!-...
    99+
    2023-01-30
    正则表达式 python
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作