1

I want to extract groups from a String like this

hkjh kha 1112266 -stsi- XXXX XXX 6616 -stsf- aa bbb 17761 jjh1 jjh1j -stsi- YYYY -stsf- kjhjk  kkj jjlklkj jljkj -stsi- YYYY YYY 123 -stsf- jkhkjh shkahska

my regexp

(-stsi-.*-stsf-)

extracts one big group

-stsi- XXXX XXX 6616 -stsf- aa bbb 17761 jjh1 jjh1j -stsi- YYYY -stsf- kjhjk  kkj jjlklkj jljkj -stsi- YYYY YYY 123 -stsf-

and I want three groups like this

-stsi- XXXX XXX 6616 -stsf-
-stsi- YYYY -stsf-
-stsi- YYYY YYY 123 -stsi-

inside -stsi- and -stsi- can be anything

Kaltresian
  • 961
  • 3
  • 14
  • 32

1 Answers1

2

Add the ? quantifier after .* so that it will match as few times as possible (.*?):

(-stsi-.*?-stsf-)

In the expression (-stsi-.*-stsf-), the character * is greedy, and it will match as many times as possible. The ? quantifier makes it lazy and prevents that.

Community
  • 1
  • 1
Josh Crozier
  • 233,099
  • 56
  • 391
  • 304