Noim
Noim

Reputation: 455

Match besides first word and words starting with one or two hyphens

I want to match any word except

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?

Upvotes: 0

Views: 89

Answers (2)

bobble bubble
bobble bubble

Reputation: 18515

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).

Upvotes: 1

Kent
Kent

Reputation: 195089

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

=\S* ([^=]*)

read the matched text in group1.

Upvotes: 0

Related Questions