Osa
Osa

Reputation: 1990

Regex - Exact match start to end

There seems to be a small confliction of my multi-line usage.. Here is the context

test-test-3
test-1

I'm trying to regex exact match each line, Giving it the test string and it returns what after -:

/test-(.*)/i

This would catch 1st which is test-test-3, I want to catch the literal second line test-1

I'm trying to make an exact match for what I'm looking for from the beginning of the line to its end ( 1 match ), Using ^ and $ is not working, Probably because I'm using multi lines (\n),

I tried this : /^test-(.*)$/i and /^test-(.*)\n$/i Which are not valid

Upvotes: 0

Views: 427

Answers (1)

Barmar
Barmar

Reputation: 780798

If you want to match the second line, you need to put a literal \n in the regexp at the beginning, because ^ matches the beginning of the first line, too.

$str = "test-test-3
test-1";
preg_match('/\ntest-(.*)$/mi', $str, $match);
var_dump($match);

DEMO

Upvotes: 1

Related Questions