正则表达式RegEx

1. 概念

  • Regular expression, 是对字符串操作的一种逻辑公式, 就是用实现定义好的一些特定字符、及这些特定字符,组成一个“规则字符串“, 这个”规则字符串“ 用来匹配字符串中的一些特定信息,达到提取信息的作用。
  • 应用场景:
  1. 表单验证(例如:手机号、邮箱、身份证…)
  2. 爬虫

2 元字符(special character)

    "."      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.

Note: (?aiLmsux) 及以下的内容不熟悉,需要再多了解

3. special sequences

  • A special sequence is a \ followed by one of the characters in the list below, and has a special meaning. Or we can say, it uses \ to escape to a difference meaning.
    \number  Matches the contents of the group of the same number.
    \A       Matches only at the start of the string. Same like "^".    "\AThe"	
    \Z       Matches only at the end of the string. Same like "$".    "Spain\Z"	
    \b       Returns a match where the specified characters are at the beginning or at the end of a word	
(the "r" in the beginning is making sure that the string is being treated as a "raw string")  r"\bain"   r"ain\b"	
    \B      Returns a match where the specified characters are present, but NOT at the beginning (or at the end) of a word	r"\Bain"     r"ain\B"	
(the "r" in the beginning is making sure that the string is being treated as a "raw string")
    \d       Matches any decimal digit; equivalent to the set [0-9] in
             bytes patterns or string patterns with the ASCII flag.
             In string patterns without the ASCII flag, it will match the whole
             range of Unicode digits.		
    \D       Matches any non-digit character; equivalent to [^\d].
    \s       Matches any whitespace character; equivalent to [   \t\n\r\f\v] in
             bytes patterns or string patterns with the ASCII flag.
             In string patterns without the ASCII flag, it will match the whole
             range of Unicode whitespace characters.
    \S       Matches any non-whitespace character; equivalent to [^\s].
    \w       Matches any alphanumeric character; equivalent to [a-zA-Z0-9_]
             in bytes patterns or string patterns with the ASCII flag.
             In string patterns without the ASCII flag, it will match the
             range of Unicode alphanumeric characters (letters plus digits
             plus underscore).
             With LOCALE, it will match the set [0-9_] plus characters defined
             as letters for the current locale.
    \W       Matches the complement of \w.
    \\       Matches a literal backslash.

这里,\number 解释下:
-例如待匹配文本为:
123abc123abc
(\d+)\w+\1 匹配结果 123abc123
(\d+)(\w+)\1\2 匹配结果 123abc123abc

4. RegEx中也用到 escaping character \.

  • 例如:“python\.com”, RE模块把\识别为特殊字符,而不是转移字符。 所以,这里并不会对 . 进行转义,而认为是 匹配任何字符。
  • 所以, RE 模块中, 如使用转义字符需要两个\,例如 “python\\.com”,或者用原始字符串r“python\.com”。 在原始字符语境中, \被认为是转义字符。
  • special character 可以用转义字符来获得normal character, 例如 \. \$

5. Sets

[arn]	Returns a match where one of the specified characters (a, r, or n) are present	
[a-n]	Returns a match for any lower case character, alphabetically between a and n	
[^arn]	Returns a match for any character EXCEPT a, r, and n	
[0123]	Returns a match where any of the specified digits (0, 1, 2, or 3) are present	
[0-9]	Returns a match for any digit between 0 and 9	
[0-5][0-9]	Returns a match for any two-digit numbers from 00 and 59	
[a-zA-Z]	Returns a match for any character alphabetically between a and z, lower case OR upper case	
[+]	In sets, +, *, ., |, (), $,{} has no special meaning, so [+] means: return a match for any + character in the string	

6. Functions

  • match(pattern, string, flags=0)
  • fullmatch(pattern, string, flags=0)
  • search(pattern, string, flags=0)
  • sub(pattern, repl, string, count=0, flags=0)
  • subn(pattern, repl, string, count=0, flags=0)
  • split(pattern, string, maxsplit=0, flags=0)
  • findall(pattern, string, flags=0)
  • finditer(pattern, string, flags=0)
  • compile(pattern, flags=0)
  • purge()
  • template(pattern, flags=0)

7. Matched object

  • search(), match()返回 matched object, finditer()返回matched object 的迭代器。
import re
txt = r"The rain in Spain"
pattern = "ain"
result = re.search(pattern, txt)
print(result)

Output:

<re.Match object; span=(5, 8), match='ain'>

The Match object has 3 properties and methods used to retrieve information about the search, and the result:

  • span() returns a tuple containing the start-, and end positions of the match.
  • string returns the string passed into the function
  • group() returns the part of the string where there was a match
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值