4

Does anyone know what is wrong with this regex? It works fine on sites like RegexPal and RegExr, but in PHP it gives me this warning and no results:

Warning: preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash

Here's my code:

preg_match('name="dsh" id="dsh" value="(.*?)"', 'name="dsh" id="dsh" value="123"', $matches);
Andrew Cheong
  • 29,362
  • 15
  • 90
  • 145
Stan
  • 25,744
  • 53
  • 164
  • 242

2 Answers2

11

You have no delimiter. Enclose the pattern in /

preg_match('/name="dsh" id="dsh" value="(.*?)"/', 'name="dsh" id="dsh" value="123"', $matches);

For patterns that include / on their own, it is advisable to use a different delimiter like ~ or # to avoid escaping:

// Delimited with # instead of /
preg_match('#name="dsh" id="dsh" value="(.*?)"#', 'name="dsh" id="dsh" value="123"', $matches);
Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390
1

You need delimiters:

preg_match('/name="dsh" id="dsh" value="(.*?)"/', 'name="dsh" id="dsh" value="123"', $matches);
Paul
  • 139,544
  • 27
  • 275
  • 264