0

So, how the title says it. How do I use regex for extracting application parameters?
I tried: --.*, but I would like it not to match unless it has any character (except whitespace) after it. So, this: --version hello would match --version, however -- hello wouldn't match anything. (Note that there is a space between -- and hello)

I wouldn't like to use any other library. (Infact I cant as I am using Flex lexer)

Any ideas?
Matthew

  • Not quite sure what you're trying to achieve but... you could take a look at [boost program options](http://www.boost.org/doc/libs/1_64_0/doc/html/program_options.html). – G.M. May 07 '17 at 17:48
  • @G.M. Sorry. I will edit. Forgot to mention I am using flex, so I cannot use any other library than pure regex. Thanks for taking a look at it though. –  May 07 '17 at 17:49
  • @Galik please search about Flex and Bison. They generate lexical analysers and parsers. The only way to get input data is to match a regex expression to the input and then you can finally output things including this. –  May 07 '17 at 18:00
  • On Windows, application command and parameters are parsed by a system process. That calls CreateProcess, filling in the argv and argc parameters. Are you getting this as a complete string before being submitted to the OS ? See [this](http://www.daviddeley.com/autohotkey/parameters/parameters.htm#WINCRULESREPH) and `(?:^[ \t]*((?>[^ \t"\r\n]+|"[^"]+(?:"|$))+)|(?!^)[ \t]+((?>[^ \t"\\\r\n]+|(?<!\\)(?:\\\\)*"[^"\\\r\n]*(?:\\.[^"\\\r\n]*)*"{1,2}|(?:\\(?:\\\\)*")+|\\+(?!"))+)|([^ \t\r\n]))` –  May 07 '17 at 23:09

1 Answers1

0

OOPS

Well, I was a bit too quick to ask here. Anyways, for anyone having the same problem, the solution is:

--\S+

or

--[^[:space:]]+

-- won't match, --a will match, and in the input --a b c, only --a will match. Cheers for anyone who was looking for a solution.

  • flex doesn't recognize `\S` as far as I know; you should use `[^[:space:]]` (or `[[:^space]]` but the first one is Posix-compliant so it will work in other regex engines.) – rici May 07 '17 at 18:22
  • @rici cheers. however, it doesn't give me an error or anything, and it works (as far as I know), but if you think this is better, then sure! Ill change it now. –  May 07 '17 at 18:24
  • It won't give you an error, in the sense that flex considers `\S` and `S` to be the same thing (in a pattern). But it shouldn't match the option. Perhaps the option is being matched by some other pattern. (If you were using jison, \S would work, I think; and there are other lex derivatives which accept such things. But not flex.) – rici May 07 '17 at 20:27