• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
在努力搬砖的jellyfish
博客园    首页    新随笔    联系   管理    订阅  订阅
【Data Preprocessing】Python使用正则表达式入门速查-Regular Expression Quick View

regular expression

This article is intended to introduce the RE package in Python.

The main funciton of RE module is

  • sub:Replace matched items.
  • match:Match from the beginning.
  • search:Search the entire string.
  • findall:Return a list of all matches.
  • finditer:Return a list of all matches.
  • split:Split a string based on a regex.
  • compile:Complie a regex for effciency.
  • escape:Escape special characters.

sub

Replace parts of the string that match the pattern wiht repl.

re.sub(pattern, repl, string, count=0, flags=0)

Demo

import re
result = re.sub(r'\d+', 'X', 'There are 6 straberries and 12 grapes')
print("Result:", result)

output:

Result: There are X straberries and X grapes.

match

Match the regular expression pattern from the beginning of the string.

re.match(pattern, string, flags=0)

Demo

import re
match = re.match(r'^Hello', 'Hello, world!')
if match:
  print("Match found:", match.group())
else:
  print("No match")

output:

Match found: Hello

search

Search the entire string for the first match of the regular expression.

re.search(pattern, string, flags=0)

Demo

import re
search = re.search(r'world', 'Hello, world!')
if search:
    print("Match found:", search.group())
else:
    print("No match")

output:

Match found: world

findall.

Return all non-overlapping matches of the pattern in the string as a list of strings.

matches = re.findall(pattern, sting, flags=0)

Demo

matches = re.findall(r'\d+', 'There are 6 strawberries and 12 grapes.')
print("Matches:", matches)

output:

Matches: ['6', '12']

finditer

Retirm an iterator yielding Match objects for all non-overlapping matches of the pattern in the string.

re.finditer(pattern, string, flags=0)

Demo

for match in re.finditer(r'\d+', 'There are 6 strawberries and 12 grapes.'):
    print("Match:", match.group(), "at position:", match.span())

output:

Match: 6 at position: (10, 11)
Match: 12 at position: (29, 31)

split

Split the string by occurrences of the pattern.

re.split(pattern, string, maxsplit=0, flags=0)

Demo

result = re.split(r'\s+', 'Hello world this is Python written by Jellyfish')
print("Result:", result)

output:

Result: ['Hello', 'world', 'this', 'is', 'Python', 'written', 'by', 'Jellyfish']

compile

Compile a regular expression pattern into a Pattern object for more efficient use.

re.compile(pattern, flags=0)

Demo

pattern = re.compile(r'\d+')
matches = pattern.findall('There are 6 strawberries and 12 grapes.')
print("Matches:", matches)

output:

Matches: ['6', '12']

escape

Escape special characters in the string.

re.escape(string)

Demo

escaped = re.escape('Hello [world]!')
print("Escaped:", escaped)

output:

Escaped: Hello\ \[world\]!
posted on 2025-03-31 13:47  汤佘  阅读(9)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3