2

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?

nhahtdh
  • 55,989
  • 15
  • 126
  • 162
  • would you mind to post one or two lines with text, and the matchings you want – Rubens Dec 29 '12 at 14:23
  • 1
    What is DS? Currently your question is quite vague, and unanswerable. Please elaborate a little more probably with some working examples. – Rohit Jain Dec 29 '12 at 14:24
  • Sorry if this was unclear. The regex already finds the correct characters - "Some DS" is just some random text. What I need is to implement the optional comma between the two existing groups. – Kristian Øllegaard Dec 29 '12 at 14:30
  • 2
    You need to use `^(?P[\w \|\-\=\&æøåÆØÅ]+)(?:,(?P[\w \|\-\=\&æøåÆØÅ,]+))?$` – Rohit Jain Dec 29 '12 at 14:45
  • EPIC, thanks a lot! I'm not a very experienced stack overflow user, but if you make an answer I can mark it as correct, if you want. – Kristian Øllegaard Dec 29 '12 at 14:48

1 Answers1

0

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
Eric
  • 95,302
  • 53
  • 242
  • 374
  • I'm not sure what the OP wants, but the OP's regex can do some preliminary validation on the input. – nhahtdh Dec 29 '12 at 15:08