2
(-[a-zA-Z]+=?)+(([^\s]+)|(\s+((["'])(?:(?=(\\?))\2.)*?\6)))

I have a bunch of command line arguments.

-arg1 -arg2 -arg3=500 -arg4=-250,200,50.0 -arg5 "Hello World" -arg6 "[I don't know whats going on /here/]"

I'm not particularly good at regex, and was using regexr.com to verify my results. Regex.Matches with the expression from this site usually works copy and pasted out no problem. This time its not.

The results of using that command line string is as follows:

Count = 6
[0]: "-arg1"
[1]: "-arg2"
[2]: "-arg3=500"
[3]: "-arg4=-250,200,50.0"
[4]: "-arg5"
[5]: "-arg6"

And this is looping through Regex.Matches and each Captures

I can't figure out why the quotes won't become part of the match. Is it maybe a whitespace issue with the |(\s+((["'] part?

user99999991
  • 1,351
  • 3
  • 19
  • 43
  • You forgot to add what you need to match/capture, what should be the output? Note the regex is very clumsy and it needs a good cleanup. – Wiktor Stribiżew Aug 25 '15 at 21:49
  • 1
    I've not done a console app in C#, but most other languages have a library. Surely there is a library available in C# that doesn't require Regex. Take a look at this thread: http://stackoverflow.com/q/491595/179311. – bradlis7 Aug 25 '15 at 21:58
  • I'm sadly not interested in using external libs! :P But thanks for anyone who comes along more interested. – user99999991 Aug 26 '15 at 01:15
  • See also http://stackoverflow.com/questions/27444640/how-to-read-regex-captures-in-c-sharp/27444808#27444808 – AdrianHHH Aug 26 '15 at 08:00

1 Answers1

3

Update:

I tweaked this per your comment. I pulled the optional space (\s*) into a non capturing group with a space OR an equal sign. You'll also see I moved the quotation capture first in the alternation since \S+ would start matching "foo and we don't want that.

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

I cleaned this up a bit in general, let me know if there's any missing functionality from the original expression you wrote:

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

Demo


There's a couple things breaking your original expression, including:

  • freaking hard to read, which results in confusion when using \2 or \6 references
  • no \d capture for the argument key (-[a-zA-Z]+=?)
  • way over-complicated logic for the capturing quoted values
Sam
  • 20,096
  • 2
  • 45
  • 71
  • Oh wow, damn that's so much better! Thanks so much. I was following the quoted values from another SO post, this is much simpler for sure. I think the reason was for innerquotes, which I dont even anticipate. Does it make it anymore complex to do something like -arg7="this works too"? In my original expression, I couldn't make another condition because I was hitting \10 reference and couldnt get anything to work with it so I originally was going to leave it. – user99999991 Aug 25 '15 at 21:55