Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Regular Expression does not match newline with s modifier

Tags:

regex

php

I am trying to match a series of words that span across 2 lines.

Say I have the following text:

this is a test
another line

My Regular Expression pattern using preg_match:

/test.*another/si

Test here: http://www.phpliveregex.com/p/2zj

PHP Pattern Modifiers: http://php.net/manual/en/reference.pcre.pattern.modifiers.php

Everything that I've read points to using the "s" modifier to enable the "." character to match new lines, but I cannot get this to work. Any ideas?

like image 200
http203 Avatar asked Feb 14 '23 09:02

http203


2 Answers

Your regular expression is correct and works fine on my local machine:

$input_line = "this is a test
another line";

preg_match("/test.*another/si", $input_line, $output_array);
var_dump($output_array);

it produces the following output:

array(1) {
  [0]=>
  string(13) "test
another"
}

So my guess is that phpliveregex.com is not working properly and giving you false results.

like image 77
byte255 Avatar answered Feb 16 '23 23:02

byte255


Put the modifier in the regex:

/(?s)test.*another/i
like image 43
elixenide Avatar answered Feb 16 '23 23:02

elixenide