user1547410
user1547410

Reputation: 893

regex to match until word but not include that keyword

I have this regex, the ouput is a bit strange, when i echo the body of the email is dont see line breaks but

When i run this:

Message:(.*?)

Instead of it returning everything after Message it returns the whole body of the email as if its ignoring line breaks

So i tried this and it worked

Message:(.*?)Issue

That captures everything from the first keyword to the last however it includes the keyword also for Issue which i dont want, how to include everything except Issue not including?

Also when i return the string i get Message: this is a test\r

Is there a better way other than running a str_replace to remove those? Im a bit of a regex virgin :)

Upvotes: 0

Views: 2323

Answers (1)

falsetru
falsetru

Reputation: 369474

Use positive forward lookahead:

preg_match('/Message:(.*?)(?=Issue)/', 'Message: blah blah Issue', $matches);
print_r($matches);

prints

Array
(
    [0] => Message: blah blah
    [1] =>  blah blah
)

Upvotes: 2

Related Questions