1

If I have the following scenario for this command line application I'm building...

update DIFFICULTY=1 DESCRIPTION="some long run on description"

I want to capture DIFFICULTY=1 and I want to capture DESCRIPTION="some long run on description" as two separate groups, and if I eventually add on more to the api, potentially another DESCRIPTION field, how would I go about doing so using regex. Casing doesn't matter so much on the left side.

I've got this so far, but it doesn't catch the whole quoted text of description, nor do I think it'd separate potential future additional description fields.

/([A-Z])([\w="])+/g

I'm using .NET cores regular expression library to figure this out as an FYI, in case there are any gotchas in js regular expressions.

ddeamaral
  • 1,403
  • 2
  • 28
  • 43
  • 1
    I think this might be useful for you (highest ranked answer): https://stackoverflow.com/questions/11429970/how-can-i-obtain-the-named-arguments-from-a-console-application-in-the-form-of-a – LocEngineer Mar 11 '19 at 12:24

2 Answers2

1

Well, yes, you can use regexes, but it would look like that:

(?<=\s)(\w+=(?:"(?:(?<=\\)"|[^"])+"|[\S]+))

(Try it here)

Not every problem should be solved using regexes though, and I suggest you see the link provided by LocEngineer on the comments.

GBrandt
  • 685
  • 4
  • 11
0

You could use a conditional regex:

\b(?P<key>[A-Z]+)=(")?(?P<value>(?(2)[^"]*|\S+))

See a demo on regex101.com.


More verbose, this means:
\b               # a word boundary
(?P<key>[A-Z]+)  # only UPPERCASE letters -> group "key"
=                # =
(")?             # capture quotes if they are present
(?P<value>       # start group "value"
    (?(2)[^"]*   # if quotes were present, match anything up to new quotes
    |            # ... or ...
    \S+)         # anything not a whitespace, 1+ times
)
Jan
  • 42,290
  • 8
  • 54
  • 79