zoukankan      html  css  js  c++  java
  • Python 学习笔记:Regular Expression

    Regular Expression (正则表达式) 是一种功能十分强大,但是又十分难以解读的古老的编程语言。通常的编程语言是以行作为最基础的解释单位,而 regular expression 则是以字符为基础解释单位。


    Regular Expression Module


    正则表达式在文本处理和文本挖掘中有很大的优势,即使是在不同编程的语言也会常常使用它。

    在 Python 中,我们可以借用 re 这个包来运用正则表达式。

    import re
    

    常用的方法有 match(), search()findall()split() 等等。其中:

    • search(): 找到第一个符合的 substring 并返回,与字符串方法 string.find() 类似。

    • findall(): 找到所有符合的 substrings 并返回 list,常常用于提取文本。

    import re
    test = "Quarantine Summary Report - Mar. 14, 23:00 for test@abc.com"
    result = re.search("S+@S+", test)
    if result:
        print("This line has an email address.")
    emails = re.findall("S+@S+", test)
    if emails:
        print(emails)
    

    Greedy matching


    对于提取文字,我们可以运用上面提到的两个方法以及正则表达式灵活进行。但是要注意 greedy matching。举个例子:

    text = "123 123 123 123"
    time = re.search("1.*3", text)
    print(time) 
    # <re.Match object; span=(0, 15), match='123 123 123 123'>
    

    在例子中符合的 substring 的组合有4种,但是由于 greedy matching 的原则,会默认取尽可能长匹配结果。如果我们只需要提取短的那一个结果,我们可以在表示 wildcard character 后加 ?,例如 *?+?, ?? 等等。

    text = "123 123 123 123"
    time = re.search("1.*?3", text)
    print(time) 
    # <re.Match object; span=(0, 3), match='123'>
    

    简单来说,greedy matching 就是指在部分字符已符合匹配后还会继续匹配,直到不成功就停止了。而与之相反的 lazy matching 就是指出现成功匹配的部分字符后就停止匹配。


    Regular Expression Quick Guide


    简单的列举一些常用的通配符。

    Syntax Description
    ^ The beginning of the line
    $ The end of the line
    . Any character
    s White space
    S Any non-whitespace
    * Repeat character zero or more times
    + Repeat character one or more times
    [] A single character in the list
    [^ ] A single character not in the list
    [0-9] Any digit
    () The position where string extraction is to begin or end
    作者:Yuki
    本文版权归作者和博客园所有,欢迎转载,转载请标明出处(附上博客链接)。 如果您觉得本篇博文对您有所收获,请点击右下角的 [推荐],谢谢!

    关注我的公众号,不定期更新学习心得
  • 相关阅读:
    secureCRT使用pem私钥
    常用的GoLang包工具
    解决vs code 调试golang时字符串显示不全的问题
    git 常用操作
    go sqlx db.Query需手动释放
    go dlv 调试
    Ambari中superset-hive认证
    HDP3.x hive load data local inpath 设置
    dolphinscheduler-调度
    sourceTree 提交错误
  • 原文地址:https://www.cnblogs.com/yukiwu/p/14537825.html
Copyright © 2011-2022 走看看