【python】【re.py】【正则表达式(RE)】

这篇博客详细介绍了Python中的正则表达式模块re.py,包括特殊字符、导入模块、公共接口和内部接口等内容。它支持8位和Unicode字符串,并提供与Perl相似的正则表达式操作。博客中提到了特殊字符的使用,以及如何通过函数进行正则匹配和操作。
摘要由CSDN通过智能技术生成
Support for regular expressions (RE).

支持正则表达式(RE)。

This module provides regular expression matching operations similar to those found in Perl.  
It supports both 8-bit and Unicode strings;  both the pattern and the strings being processed can contain null bytes and characters outside the US ASCII range.

这个模块提供了类似于Perl中的正则表达式匹配操作。

它支持8位和Unicode字符串;正在处理的模式和字符串都可以包含空字节和超出美国ASCII范围的字符。

Regular expressions can contain both special and ordinary characters.

Most ordinary characters, like "A", "a", or "O", are the simplest regular expressions; they simply match themselves.

You can concatenate ordinary characters, so last matches the string 'last'.

正则表达式可以包含特殊字符和普通字符。

大多数普通字符,如“A”、“A”或“O”,都是最简单的正则表达式;他们只是匹配自己。

您可以连接普通字符,因此last匹配字符串'last'。

1 特殊字符 The special characters are:

    "."      Matches any character except a newline.
    "^"      Matches the start of the string.
    "$"      Matches the end of the string or just before the newline at
             the end of the string.
    "*"      Matches 0 or more (greedy) repetitions of the preceding RE.
             Greedy means that it will match as many repetitions as possible.
    "+"      Matches 1 or more (greedy) repetitions of the preceding RE.
    "?"      Matches 0 or 1 (greedy) of the preceding RE.
    *?,+?,?? Non-greedy versions of the previous three special characters.
    {m,n}    Matches from m to n repetitions of the preceding RE.
    {m,n}?   Non-greedy version of the above.
    "\\"     Either escapes special characters or signals a special sequence.
    []       Indicates a set of characters.
             A "^" as the first character indicates a complementing set.
    "|"      A|B, creates an RE that will match either A or B.
    (...)    Matches the RE inside the parentheses.
             The contents can be retrieved or matched later in the string.
    (?aiLmsux) Set the A, I, L, M, S, U, or X flag for the RE (see below).
    (?:...)  Non-grouping version of regular parentheses.
    (?P<name>...) The substring matched by the group is accessible by name.
    (?P=name)     Matches the text matched earlier by the group named name.
    (?#...)  A comment; ignored.
    (?=...)  Matches if ... matches next, but doesn't consume the string.
    (?!...)  Matches if ... doesn't match next.
    (?<=...) Matches if preceded by ... (must be fixed length).
    (?<!...) Matches if not preceded by ... (must be fixed length).
    (?(id/name)yes|no) Matches yes pattern if the group with id/name matched,
                       the (optional) no pattern otherwise.
"."      Matches any character except a newline.
         匹配除换行符以外的任何字符。

"^"      Matches the start of the string.
         匹配字符串的开头。

"$"      Matches the end of the string or just before the newline at
         the end of the string.
         匹配字符串的末尾字符串末尾换行符之前。

"*"      Matches 0 or more (greedy) repetitions of the preceding RE.
         Greedy means that it will match as many repetitions as possible.

"+"      Matches 1 or more (greedy) repetitions of the preceding RE.
         匹配前一个正则的1个或多个(贪婪)重复。

"?"      Matches 0 or 1 (greedy) of the preceding RE.
         匹配前一个正则的0或1(贪婪)。

*?,+?,?? Non-greedy versions of the previous three special characters.
         前三个特殊字符的非贪婪版本。

{m,n}    Matches from m to n repetitions of the preceding RE.
{m,n}?   Non-greedy version of the above.
"\\"     Either escapes special characters or signals a special sequence.
[]       Indicates a set of characters.
         A "^" as the first character indicates a complementing set.
"|"      A|B, creates an RE that will match either A or B.
         A|B,创建一个匹配A或B的正则。

(...)    Matches the RE inside the parentheses.
         The contents can be retrieved or matched later in the string.
(?aiLmsux) Set the A, I, L, M, S, U, or X flag for the RE (see below).
(?:...)  Non-grouping version of regular parentheses.
(?P<name>...) The substring matched by the group is accessible by name.
(?P=name)     Matches the text matched earlier by the group named name.
(?#...)  A comment; ignored.
(?=...)  Matches if ... matches next, but doesn't consume the string.
(?!...)  Matches if ... doesn't match next.
(?<=...) Matches if preceded by ... 
  • 9
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

资源存储库

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值