0

How can I parse a string using regex?

I want to find is string an option or not

--option ABC -o DEF -help TRUE -h FALSE

I tried this

"--option".matches("(--)(\\s+)");

But this is an invalid regex. Any ideas how to find if string is an option?

barbara
  • 3,111
  • 6
  • 30
  • 58
  • 2
    See [this](http://stackoverflow.com/questions/367706/is-there-a-good-command-line-argument-parser-for-java) thread from cli option parsers. It is the better way to work with it. – Jens Jan 08 '15 at 18:53
  • Yes, I know. But I want to write my own parser. – barbara Jan 08 '15 at 18:55
  • What do you mean that it's an invalid regex? It looks fine to me, and when I tested it I didn't get an exception or anything? – ajb Jan 08 '15 at 18:55
  • @ajb It doesn't parse a string correct. – barbara Jan 08 '15 at 18:55
  • OK, then it's an incorrect regex. An "invalid regex" is one that doesn't obey the rules for how to form a regex. This one obeys the rules but it's just wrong. Your regex matches two hyphens followed by any number of _whitespace_ characters (that's what `\\s` means), but it will fail if there are any letters or anything else following the `--`. – ajb Jan 08 '15 at 18:57

1 Answers1

2

If you want to parse strings of the form:

-option

or

--option

you can try this:

-{1,2}\\w+

Regular expression visualization

Debuggex Demo

Notes:

  • The part {1,2} means that the - should be repeated at least 1 time, but no more than 2 times.
  • \\w+: match any non-null strings of alphanumeric characters

Edit: If you want to match only characters (no numbers), then as suggested, try:

-{1,2}[A-Za-z]+

Regular expression visualization

Debuggex Demo

Christian Tapia
  • 33,620
  • 7
  • 56
  • 73
  • It matches `-1` or `--5` as well. But it should be match to characters `(--V or -s)` – barbara Jan 08 '15 at 19:01
  • 1
    @barbara If this is incorrect, then you need to be specific about what kinds of strings you want to match. You didn't really provide that information in your question. (And on Unix, at least, there are commands for which `-1` is a valid option, such as `head`.) – ajb Jan 08 '15 at 19:03
  • @barbara `1` and `5` _are_ characters. – ajb Jan 08 '15 at 19:03
  • If you want only letters, then change `\\w` to `[A-Za-z]`. – ajb Jan 08 '15 at 19:07