2017-07-27 49 views
-1

今天我碰到这行代码:无法理解re.findall模式语法

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

我什么模式findall功能正在寻找非常困惑。具体而言,r"#[^:]+:([^#]+)"是什么意思?

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

+2

您应该开始阅读[documentation](https://docs.python.org/2/library/re.html)并更新您的问题。 –

+0

作为阅读文档的补充,您可以测试您的正则表达式并获得关于它如何与[regex101.com](https://regex101.com)一起使用的解释。 –

+0

我应该如何更新它? – Joe

回答

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文本,这意味着它里面什么没有任何特殊含义的编译器和你不必须逃避任何字符,甚至双引号!

+0

这看起来像是对这个正则表达式的[regex101的解释](https://regex101.com)的轻微修改。 OP将指出这一点会有所帮助。 –

+0

非常感谢 – Joe

+0

如果'str =“#个k点:816#个带:52#个离子:8个''函数返回什么?它会成为“K点数:816”还是“816”? – Joe