无法理解re.findall模式语法

时间:2017-07-27 03:36:33

标签: python-2.7 findall

今天我遇到了这行代码:

re.findall(r"#[^:]+:([^#]+)", str)

我对findall函数正在寻找的模式感到困惑。具体来说r"#[^:]+:([^#]+)"是什么意思?

我是一名高中生,所以如果你能用简单的术语解释那就太棒了!

1 个答案:

答案 0 :(得分:3)

这意味着:

# => matches the character # literally (case sensitive)

[^:] => Match a single character that is not :

+ => Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy) and is applied to the [^:]

: => matches the character : literally (case sensitive)

([^#]+) => Capturing Group

    [^#] => Match a single character not present in this list (match anything other than #)

    + => Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy) and is applied to [^#]

请注意r字面值表示引用的字符串是raw文本,这意味着其中的任何内容对编译器没有任何特殊含义,您不必转义任何字符甚至双引号!