Nabil Farhan
Nabil Farhan

Reputation: 1536

How to use Regex to find exact same sequence in php?

I want to use regex to delete all characters after the exact sequence 'roast'. I tried the following.

echo preg_replace('/^[^roast]+/', '', "I ate a big roast and it was delicious.");

However, if there is one character from the list is found;not a sequence as a whole then regex is returning a match.In this case it is getting the 'a' of 'ate' as a match.

What I want: " and it was delicious"

What I am getting: "ate a big roast and it was delicious."

Upvotes: 0

Views: 30

Answers (1)

Ryan M
Ryan M

Reputation: 20167

Your current regex matches a sequence of characters other than r, o, a, s, or t, at the start of the string.

You want any string, followed by roast:

.+roast

Demo: https://regex101.com/r/6gFES8/3

Upvotes: 1

Related Questions