专业编程基础技术教程

网站首页 > 基础教程 正文

Python基础知识_正则表达式(re) python 正则表达

ccvgpt 2024-10-12 13:44:57 基础教程 5 ℃

Python 的正则表达式(Regular Expressions,简称 regex)是通过 re 模块来实现的。正则表达式是一种强大的文本处理工具,可以用来进行搜索、替换、匹配等操作。以下是一些常用的 re 模块功能及其示例:

1.导入re模块

import re

2. 编写正则表达式

正则表达式是一种模式描述语言,用于匹配字符串中的字符组合。在Python中,正则表达式通常作为字符串传递给re模块的函数。

Python基础知识_正则表达式(re) python 正则表达

3. 使用re模块的函数

re模块提供了多个函数来处理正则表达式,其中最常用的包括:

  • re.match(pattern, string, flags=0): 尝试从字符串的起始位置匹配正则表达式。
  • re.search(pattern, string, flags=0): 扫描字符串,返回第一个匹配正则表达式的子串的Match对象。
  • re.findall(pattern, string, flags=0): 返回字符串中所有匹配正则表达式的子串的列表。
  • re.sub(pattern, repl, string, count=0, flags=0): 使用repl替换字符串中所有匹配正则表达式的子串。
  • re.compile(pattern, flags=0): 编译一个正则表达式模式,返回一个Pattern对象。

4. 处理匹配结果

如果匹配成功,re.match()、re.search()和re.finditer()会返回一个Match对象,你可以使用这个对象来获取匹配的详细信息,比如匹配的子串、匹配的位置等。

基本匹配

1. 匹配字符串

	pattern = r'hello'  

	text = 'hello world'  

	match = re.search(pattern, text)  

	if match:  

	    print('Match found:', match.group())  

	else:  

	    print('No match found')

2. 匹配数字

	pattern = r'\d+'  

	text = 'There are 123 apples'  

	match = re.search(pattern, text)  

	if match:  

	    print('Match found:', match.group())  

	else:  

	    print('No match found')

特殊字符

1..匹配任意字符(除了换行符)


	pattern = r'a.c'  

	text = 'abc abc123'  

	match = re.search(pattern, text)  

	if match:  

	    print('Match found:', match.group())

2.^匹配字符串的开始

python
复制代码
	pattern = r'^hello'  

	text = 'hello world'  

	match = re.search(pattern, text)  

	if match:  

	    print('Match found:', match.group())

3.$匹配字符串的结束

python
复制代码
	pattern = r'world#39;  

	text = 'hello world'  

	match = re.search(pattern, text)  

	if match:  

	    print('Match found:', match.group())

字符集

1.[abc]匹配方括号内的任意一个字符

python
复制代码
	pattern = r'[abc]'  

	text = 'abc def'  

	match = re.search(pattern, text)  

	if match:  

	    print('Match found:', match.group())

2.[a-z]匹配方括号内的字符范围

python
复制代码
	pattern = r'[a-z]'  

	text = 'A quick brown fox'  

	match = re.search(pattern, text)  

	if match:  

	    print('Match found:', match.group())

量词

1.*匹配前面的字符零次或多次

python
复制代码
	pattern = r'a*b'  

	text = 'aaab b'  

	match = re.search(pattern, text)  

	if match:  

	    print('Match found:', match.group())

2.+匹配前面的字符一次或多次

python
复制代码
	pattern = r'a+b'  

	text = 'aab b'  

	match = re.search(pattern, text)  

	if match:  

	    print('Match found:', match.group())

3.?匹配前面的字符零次或一次

python
复制代码
	pattern = r'a?b'  

	text = 'ab b'  

	match = re.search(pattern, text)  

	if match:  

	    print('Match found:', match.group())

4.{n}匹配前面的字符恰好 n 次

python
复制代码
	pattern = r'a{3}b'  

	text = 'aaab b'  

	match = re.search(pattern, text)  

	if match:  

	    print('Match found:', match.group())

分组和捕获

1. 使用圆括号进行分组

python
复制代码
	pattern = r'(\d+)-(\d+)-(\d+)'  

	text = 'My birthday is 1990-01-01'  

	match = re.search(pattern, text)  

	if match:  

	    print('Match found:', match.group(0))  # 整个匹配  

	    print('Year:', match.group(1))  

	    print('Month:', match.group(2))  

	    print('Day:', match.group(3))

替换

1. 使用re.sub进行替换

python
复制代码
	pattern = r'\d+'  

	text = 'There are 123 apples and 456 oranges'  

	new_text = re.sub(pattern, 'XXX', text)  

	print(new_text)  # 输出: There are XXX apples and XXX oranges

编译正则表达式

1. 使用re.compile编译正则表达式

python
复制代码
	pattern = re.compile(r'\d+')  

	text = 'There are 123 apples'  

	match = pattern.search(text)  

	if match:  

	    print('Match found:', match.group())

查找所有匹配项

1. 使用re.findall查找所有匹配项

python
复制代码
	pattern = r'\d+'  

	text = 'There are 123 apples and 456 oranges'  

	matches = re.findall(pattern, text)  

	print(matches)  # 输出: ['123', '456']

忽略大小写

1. 使用re.IGNORECASE忽略大小写

python
复制代码
	pattern = r'hello'  

	text = 'Hello World'  

	match = re.search(pattern, text, re.IGNORECASE)  

	if match:  

	    print('Match found:', match.group())

多行匹配

1. 使用re.MULTILINE进行多行匹配

python
复制代码
	pattern = r'^hello'  

	text = '''hello  

	world  

	hello again'''  

	matches = re.findall(pattern, text, re.MULTILINE)  

	print(matches)  # 输出: ['hello', 'hello']

Tags:

最近发表
标签列表