Python 2.7 难以理解re.findall模式语法

Python 2.7 难以理解re.findall模式语法,python-2.7,findall,Python 2.7,Findall,今天我遇到了这一行代码: re.findall(r"#[^:]+:([^#]+)", str) 我对findall函数所寻找的模式感到非常困惑。具体来说,r“#[^::+:([^#]+)”是什么意思 我是一名高中生,如果你能简单地解释一下,那就太棒了 它的意思是: # => matches the character # literally (case sensitive) [^:] => Match a single character that is not : + => Quanti

今天我遇到了这一行代码:

re.findall(r"#[^:]+:([^#]+)", str)
我对
findall
函数所寻找的模式感到非常困惑。具体来说,
r“#[^::+:([^#]+)”
是什么意思

我是一名高中生,如果你能简单地解释一下,那就太棒了

它的意思是:

# => 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
literal意味着引用的字符串是一个
raw
文本,这意味着其中的任何内容对编译器没有任何特殊意义,您不必转义任何字符甚至双引号

你应该开始阅读并更新你的问题。作为对阅读文档的补充,你可以测试你的正则表达式,并得到一些关于它如何工作的解释。我应该如何更新它?@Thierrylahuille感谢它帮助了很多的网站。这看起来像是对这个正则表达式的轻微编辑。这将有助于OP指出这一点。非常感谢您。str=“#of k点:816#of带:52#of离子:8”函数返回什么?是k点中的“816”,还是仅仅是“816”?@thierrylahuille是的。我确实理解这个模式,并希望自己提供它,但由于时间不够,我选择了这个有用的应用程序,用它让我的生活更轻松。@Joe这是正则表达式吗?