简体   繁体   中英

Python3 regex issue

I'm writing a solution to a problem in which I need to parse the command line arguments. Before parsing, I first did the validation.

Permissible arguments are:

someKey=(apps IN (app1))
someKey=(apps IN (app1,app2))
someKey=(apps IN (app1,app2, app3))

But if comma is at the end of last app name, then I want the validation to fail.

someKey=(apps IN (app1,app2,))

I wrote the regex as follows.

\(apps\sIN\s\((app\d|,)+\)\)

But it taking both valid and invalid cases. I got some references regarding look ahead and look behind lookup, but failed to implement it correctly. Can any of you guys help me to understand what is the resolution to this problem?

You can use \\(apps\\sIN\\s\\((app\\d+)(,\\s*app\\d+)*\\)\\)

This will ensure that at least one occurrence of app\\d+ exists, and every subsequent occurrence has to be preceded by a comma (and optional whitespace).

Demo

If you need to capture the app names, you can use:

\(apps\sIN\s\((app\d+)((?:,\s*(?:app\d+))*)\)\)

and you will have the first app name in capture group 1, and the rest - including the commas - in capture group 2, which would then need to be split by the comma and have the spaces strip ped off.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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