0

I'm not big fan of regex but this time I think that there is no way to do what i want. Please see my example with real live testing: (BEGIN @\d+\b.*?ACTION_READLN ~.+?~) Consider such raw text data:

BEGIN @1011 Text
Text
    ACTION_READLN ~variableName~
Text
END
BEGIN @1012
asdasd
    ACTION_READLN ~someothervariable~
END
BEGIN @1013

asd
END

I was able to create regex which finds "BEGIN @(only numbers)" and "ACTION_READLN ~*~"

BEGIN @\d+|(ACTION_READLN.~.+~)

Now, I want to group first occurrence of BEGIN with first occurrence of ACTION_READLN. How i can do this?

EDIT: Expected results:

Group1:
BEGIN @1011
ACTION_READLN ~variableName~

Group2:
BEGIN @1012
ACTION_READLN ~someothervariable~
ALIENQuake
  • 520
  • 3
  • 12
  • 28

1 Answers1

1

Use s flag only in between for dot to consume newlines. Groups will capture needed stuff.

$re = '/(BEGIN @\d+)\b(?s:.*?)(ACTION_READLN.*)/';

See demo at regex101. With preg_match_all use PREG_SET_ORDER to set output mode if desired.

preg_match_all($re, $str, $out, PREG_SET_ORDER);

Captured matches in each $out will be elements [1] and [2]. [0] the full match. Try at eval.in

bobble bubble
  • 16,888
  • 3
  • 27
  • 46