正则表达式介绍——基于python

正则表达式介绍——基于python

本blog基于UC Berkeley课程CS61A

正则表达式介绍

正则表达式是不依赖于任何语言的,通用的declarative language[生命性语言]

主要语法

符号含义使用说明
.(dot)可匹配任意一个字符如banana可以匹配r’.a.a.a’
[]匹配方括号集合内的任意一个字符如t可以匹配[r’top]’
\d匹配任意一个数字字符,等价于[0-9]如12可以匹配r’\d\d’
\w匹配任意一个字符,等价于[0-9A-Za-z]如4Z可以匹配r’\d\w’
\s匹配任意一个空白字符,如spaces、tabs、line breaks如9 a可以匹配r’\d\s\w’
*将前面的模块匹配 ≥ 0 \geq0 0如aaa可以匹配r’a*’
+将前面的模块匹配 ≥ 1 \geq1 1如lool可以匹配r’lo+l’
?将前面的模块匹配0或1次如lol可以匹配r’lo?l’
{}用法为{min, max},将前面的模块匹配 ≥ \geq min、 ≤ \leq max次如:aa匹配r’a{2}‘、aaaaa可以匹配r’a{2,}’、aaa可以匹配r’a{2,4}’
|匹配其中之一如Inf可以匹配r’\d+
()无论括号内的字符是什么,匹配括号内的字符如< 3< 3< 3可以匹配r’(< 3)+’
^匹配一个字符串的开头如aww可以匹配r’aw+’
$匹配一个字符串的末尾如stay可以匹配r’\w+y$’
\b匹配一个word的边界如bridge可以匹配r’\w+e\b’

python案例

在python中re模块可以支持处理字符串,在课程中有三个小case,这里使用re模块进行实现.

  • 识别正确的引用
def scientific_name(name):
    """
    Returns True for strings that are in the correct notation for scientific names;
    i.e. contains a capital letter followed by a period or lowercase letters, 
    followed by a space, followed by more lowercase letters. Returns False for 
    invalid strings.

    >>> scientific_name("T. rex")
    True
    >>> scientific_name("t. rex")
    False
    >>> scientific_name("tyrannosurus rex")
    False
    >>> scientific_name("t rex")
    False
    >>> scientific_name("Falco peregrinus")
    True
    >>> scientific_name("F peregrinus")
    False
    >>> scientific_name("Annie the F. peregrinus")
    False
    >>> scientific_name("I want a pet T. rex right now")
    False
    """
    return bool(re.search(r'^[A-Z]([a-z]+|[.])\s[a-z]+$', name))
  • 识别正确的运算格式
def calculator_ops(calc_str):
    """
    Returns True if an expression from the Calculator language that has two
    numeric operands exists in calc_str, False otherwise.

    >>> calculator_ops("(* 2 4)")
    True
    >>> calculator_ops("(+ (* 3 (+ (* 2 4) (+ 3 5))) (+ (- 10 7) 6))")
    True
    >>> calculator_ops("(* 2)")
    False
    >>> calculator_ops("(/ 8 4 2)")
    False
    >>> calculator_ops("(- 8 3)")
    True
    >>> calculator_ops("+ 3 23")
    False
    """
    return bool(re.search(r'^\(([-+*/]\s+\d+\s+\d+)\)', calc_str))
  • 识别正确的罗马数字
def roman_numerals(text):
    """
    Returns True if any string of letters that could be a Roman numeral
    (made up of the letters I, V, X, L, C, D, M) is found. Returns False otherwise.

    >>> roman_numerals("Sir Richard IIV, can you tell Richard VI that Richard IV is on the phone?")
    True
    >>> roman_numerals("My TODOs: I. Groceries II. Learn how to count in Roman IV. Profit")
    True
    >>> roman_numerals("I. Act 1 II. Act 2 III. Act 3 IV. Act 4 V. Act 5")
    True
    >>> roman_numerals("Let's play Civ VII")
    True
    >>> roman_numerals("i love vi so much more than emacs.")
    False
    >>> roman_numerals("she loves ALL editors equally.")
    False
    """
    return bool(re.search(r'\b([IVXLCDM]+)\b', text))
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值