CodeIgniter Forums
regular expression is not working - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: regular expression is not working (/showthread.php?tid=42091)



regular expression is not working - El Forum - 05-26-2011

[eluser]samitrimal[/eluser]
Hi i have a problem in using dollor sign in my regular expression . When i dont use dollar sign it works fine.Here is a code
Code:
$pattern="/^[^0-9][0-5a-zA-Z]{1,}([a-zA-Z0-9_|-])*$/";
if (preg_match($pattern, "a12-|", $matches)) {
  echo "Match was found <br />";
  echo $matches[0];
}
else{
  echo "Pattern does not match";
}
The error occours when i use dollor sign.
Code:
$pattern="/^[^0-9][0-5a-zA-Z]{1,}([a-zA-Z0-9_|-$])*$/";
if (preg_match($pattern, "a12-|$", $matches)) {
  echo "Match was found <br />";
  echo $matches[0];
}
else{
  echo "Pattern does not match";
}
I encountered the following error
Code:
Warning: preg_match() [function.preg-match]: Compilation failed: range out of order in character class at offset 36 in E:\wamp\www\graph\testpractice\php\regex\pregmath.php on line 3
need help to solve this problem


regular expression is not working - El Forum - 05-26-2011

[eluser]theprodigy[/eluser]
Quote:The error occours when i use dollor sign.
Code:
$pattern="/^[^0-9][0-5a-zA-Z]{1,}([a-zA-Z0-9_|-$])*$/";
if (preg_match($pattern, "a12-|$", $matches)) {
  echo "Match was found <br />";
  echo $matches[0];
}
else{
  echo "Pattern does not match";
}
The $ is a special character in Regex.
Try putting a backslash before the $ in your $pattern;
Code:
$pattern="/^[^0-9][0-5a-zA-Z]{1,}([a-zA-Z0-9_|-\$])*$/";
if (preg_match($pattern, "a12-|$", $matches)) {
  echo "Match was found <br />";
  echo $matches[0];
}
else{
  echo "Pattern does not match";
}



regular expression is not working - El Forum - 05-26-2011

[eluser]InsiteFX[/eluser]
Here is a good regular expression utility that I found!

Regular Expression tester

If you look at the bottom right side you can download a desktop version of it.

InsiteFX


regular expression is not working - El Forum - 05-26-2011

[eluser]samitrimal[/eluser]
thank u InsiteFX this tool is great and everyone .