美文网首页给流浪的文字安个家缘分的天空犁 苑 深 声
三分钟热情自学 Python · 第10期 · Python 正

三分钟热情自学 Python · 第10期 · Python 正

作者: 游文影月志 | 来源:发表于2020-06-20 20:09 被阅读0次

    千淘万漉虽辛苦,吹尽狂沙始到金。
    ——刘禹锡

    1. 前言

    在这个生活中处处都是大数据和人工智能的时代,总是能在各种角落看到 Python 的培训广告。我招架不住敌方的猛烈攻势,败下阵来。经过了一分钟的深思熟虑,我决定利用我的三分钟热情进行回击,从零开始自学 Python

    本期要学习的正则表达式颇有些难度,如果要用一个词来形容它,那非人类语言就再恰当不过了。因此本期我就蜻蜓点水式地学习一下,等以后再回过头来好好学习这一块。

    2.

    Regular Expressions

    正则表达式

    A regular expression is a group of characters or symbols which is used to find a specific pattern in a text.

    正则表达式是由字母和符号组成的一组字符,它可以用来从文本中找出符合某种模式的字符串。

    2.1

    Steps to Using Regular Expressions in Python

    在 Python 中使用正则表达式的步骤

    1. Import the regex module with import re.

      使用 import re 引入正则表达式模块。

      In Python, you need to import the re module first to creating a Regex object. Otherwise, you'll get a NameError: name 're' is not defined error message.

    在 Python 中,需要先引入 re 模块才能创建 Regex 对象。否则将会得到报错信息:“NameError: name 're' is not defined error message”。

    1. Create a Regex object with the re.compile() function.

      使用 re.compile() 函数创建一个 Regex 对象,即正则表达式对象。

    1. Pass the string you want to search into the Regex object's search() method. This returns a Match object.

      将要搜索的字符串传递给 Regex 对象的 search() 方法。 该方法将会返回一个 Match 对象。

    1. Call the Match object's group() method to return a string of the actual matched text.

      调用 Match 对象的 group() 方法,以返回文本中所匹配的字符串。

    myPattern = re.compile(r'^\d{3}$')
    # => NameError: name 're' is not defined
    
    import re  # step 1
    
    myPattern = re.compile(r'^\d{3}$')  # step 2
    matchObj = myPattern.search('123')  # step 3
    matchStr = matchObj.group()  # step 4
    print(matchStr)
    # => 123
    

    2.2

    Online Regex Tester

    正则表达式在线测试工具

    https://regex101.com/

    进入页面,在 Regex Editor 中选择编程语言为 Python。在 Regular Expression 处输入要测试的正则表达式,而下方的 Test String 中则输入被用来搜索匹配的文本。

    点击左侧的 Settings 可以切换页面的语言和主题。

    2.3

    Python Regex Cheatsheet

    Python 正则表达式速查表

    Regex Meaning
    ^ Start of string
    $ End of string
    ab The string ab
    a|b a or b
    [abc] A single character of: a, b or c
    [^abc] A character except: a, b or c
    [a-z] A character in the range: a-z
    \d One digit
    \D One non-digit
    \s Any whitespace character
    \S Any non-whitespace character
    \w One word character
    \W One non-word character
    \b A word boundary
    \B Non-word boundary
    \d? Zero or one of digit
    \d* Zero or more of digit
    \d+ One or more of digit
    \d{3} Exactly 3 of digit
    \d{3,} 3 or more of digit
    \d{3,6} Between 3 and 6 of digit
    . Any character except newline

     


     

    正则 含义
    ^ 从字符串起始处匹配
    $ 从字符串结尾处匹配
    ab 匹配字符串 ab
    a|b 匹配字母 ab
    [abc] 匹配 abc 其中一个字母
    [^abc] 匹配除 abc 以外任一字符
    [a-z] 匹配从 az 任意一个字母
    \d 匹配一个数字
    \D 匹配一个非数字
    \s 匹配任意空白字符
    \S 匹配非空白字符
    \w 匹配英文字母、数字或下划线,等价于 [a-zA-Z0-9_]
    \W 匹配除字母、数字或下划线以外的字符,等价于 [^a-zA-Z0-9_]
    \b 匹配单词边界
    \B 匹配非单词边界
    \d? 匹配 0 或 1 个数字
    \d* 匹配 0 或多个数字
    \d+ 匹配 1 或多个数字
    \d{3} 匹配 3 个数字
    \d{3,} 匹配 3 或多个数字
    \d{3,6} 匹配 3 或 6 个数字
    . 匹配除换行符外任一字符

    3. 本期单词学习

    regular expression /ˈreɡjələr ɪkˈspreʃn/
    n. 正则表达式

    4. 小结

    仅用寥寥可数的几个字符,就能匹配各种错综复杂的字符排列模式,以简驭繁,事半功倍,这就是正则表达式的强大之处,也是我必须学会使用它的主要原因。

    相关文章

      网友评论

        本文标题:三分钟热情自学 Python · 第10期 · Python 正

        本文链接:https://www.haomeiwen.com/subject/vtlgxktx.html