0

I want to match any word except

  • the first word
  • anything that matches (-(-)?\w*(=\w*)?)

Example input:

test --par2 --par23=whatever 0 xd true hello "string test"
test --par23=whatever 0 xd true hello --par2 "string test"
test 0 xd "string test" true hello --par2 --par23=whatever

What I want is:

0, xd, true, hello, "string test"  

I don't want: test, --par2 and --par23=whatever Language: JavaScript

What would be a good idea?

Noim
  • 445
  • 1
  • 3
  • 20

2 Answers2

1

See The Trick. How about matching what you don't need but capturing what you need.

don't want

  • ^\S+ one or more non whitespaces at start (\S is negation of short \s)
  • \W-\S+ strings like -foo, --bar but want bar-baz (\W for non word character)

but ( capture )

  • "[^"]+" any quoted stuff (using negated class)
  • \S+ one or more remaining non whitespaces

Order according matching priority and combine with pipes |

("[^"]+")|^\S+|\W-\S+|(\S+)

See demo at regex101. Grab captures of the two groups like in this fiddle.

[ "0", "xd", "true", "hello", ""string test"" ]

If using PCRE you can skip the unneeded stuff by use of verbs (*SKIP)(*F).

Community
  • 1
  • 1
bobble bubble
  • 16,888
  • 3
  • 27
  • 46
  • It works. But if you would add `-t` or something like that i would also captured. The problem is, i don't understand how it works :D – Noim Dec 27 '16 at 17:29
  • @NilsBergmann did [another update](https://jsfiddle.net/pw7krcj9/5/) for matching [`"quoted str"` if at start](https://www.regex101.com/r/nTpeDL/1). Using two capturing groups. Hope this works for you! – bobble bubble Dec 27 '16 at 18:20
0

I hope I understood you right, do you mean this?

=\S* ([^=]*)

read the matched text in group1.

Kent
  • 189,393
  • 32
  • 233
  • 301
  • Not really. It simply match everything after ...--par23. But the string could also be `test 0 xd true hello "string test" --par2 --par23=whatever` or `test 0 xd true hello --par2 "string test" --par23=whatever` – Noim Dec 27 '16 at 14:02
  • @NilsBergmann don't know which programming language are you using. If you are handling arguments, I don't think regex is a best way to go. E.g., your argument value could have `=` and `-`, `--` as well. There could be Argument Parsers available. Use them if you find some. – Kent Dec 27 '16 at 14:06
  • I don't want to use a parser. And parameters are fine to mach with (-(-)?\w*(=\w*)?) My only problem is to mach anything else. Except the first word. – Noim Dec 27 '16 at 14:13