简体   繁体   English

正则表达式捕获了正向后看的组(python)

[英]Regex captured groups with positive lookbehind (python)

How is it possible to combine a captured group with positive lookbehinds? 怎样才能将捕获到的组与正向后方相结合?

I want to match the following examples: 我想匹配以下示例:

DS
DS,x=y
Some DS,key=value
Some DS test,key=value&key2=value2
Some DS test,key=value&key2=value2|key3=value3

I came up with the following regex for matching everything but the comma: 我想出了以下正则表达式来匹配除逗号以外的所有内容:

^(?P<ds_title>[\w \|\-\=\&æøåÆØÅ]+)(?P<filters>[\w \|\-\=\&æøåÆØÅ]+)?$

I've figured out, that the regex I would need to insert is: 我已经知道,我需要插入的正则表达式是:

(?<=\,)

But I cannot figure out where to insert it. 但我不知道在哪里插入它。 No matter where I insert it, it seems to break. 无论我在哪里插入,它似乎都坏了。 Does anyone know how this can be done correctly? 有谁知道如何正确地做到这一点?

Aren't you overthinking this? 你不是在想这个吗?

^(?P<ds_title>[^,]+)(?:,(?P<filters>.+))?$

Why not just allow any string, rather than limiting it to your list? 为什么不只允许任何字符串,而不是将其限制在列表中?


In fact, why use a regex at all? 实际上,为什么要使用正则表达式呢?

parts = data.split(',', 2)
if len(parts) == 1:
    title, = parts
else:
    title, filters = parts

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM