Asara
Asara

Reputation: 3374

php regex catch text after

I have this regex:

$pattern = '!(>+\h+)?(.*)schrieb:(.*?)!iUgs';

to match

Daniel schrieb:
> I think 

That works. But when I have more than one matching target

Daniel schrieb:
> I think 

Hi,
Jessi schrieb:
> I do
test

the second match will end up in the last catching group, but it should be matched again (but the whole followup, including the second match should be in the last catching group). Is that possible?

I Want to capture

1: Daniel

and

2: 
> I think 

Hi,
Jessi schrieb:
> I do
test

and after that

1: Jesse

and

2:
> I do
test

See: https://regex101.com/r/64Rr52/2

Upvotes: 1

Views: 50

Answers (1)

anubhava
anubhava

Reputation: 784998

You can use this regex in PHP:

~^(.*?)\h+schrieb:(?=((?s).*\z))~im

(?=((?s).*\z)) is a lookahead that does lookahead assertion and groups text after schrieb: till end of file.

RegEx Demo

Upvotes: 1

Related Questions