3

how can i extract bold range string in below

string :

  1. hello world blah -d blah vlaah -n blah vlahh
  2. hello world blah -n blah vlahh -d blah vlaah
  3. hello world blah -d blaaah

I tried. -[dn] .*$ but it found longest match string like below

  1. hello world blah -d blah vlaah -n blah vlahh

I want to extract shortest match string . thanks in advance

Community
  • 1
  • 1
kimwz.kr
  • 298
  • 1
  • 9

2 Answers2

5

You can use a negative lookahead to avoid matching another -d/-n in the match:

-[dn] (?!.*?-[dn]).*$

RegEx Demo

anubhava
  • 761,203
  • 64
  • 569
  • 643
2

Could throw a greedy .* before to eat up:

^.*(-[dn] .*)$

And grab matches of the first capture group. See test at regex101

Jonny 5
  • 12,171
  • 2
  • 25
  • 42