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);
slackwing
  • 27,451
  • 15
  • 82
  • 136
Stan
  • 24,526
  • 50
  • 156
  • 238

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
  • 260,803
  • 45
  • 432
  • 377
1

You need delimiters:

preg_match('/name="dsh" id="dsh" value="(.*?)"/', 'name="dsh" id="dsh" value="123"', $matches);
Paul
  • 135,475
  • 25
  • 268
  • 257