PHP RegEx - no ending delimiter? -


this question has answer here:

i new regex , felt got hang of basics when working .htaccess file. working on form validation check user inputs valid serial number system. system can accept following serial format.

  • i-serial-123
  • i-serial123
  • serial-123
  • serial123

i using preg_match function check whether expression satisfied, if field submitted.

current expression

if (preg_match("^[a-z0-9\-]{5}$", $_get['serial']) === false)  

however php keeps throwing "no ending delimiter found" exception, i've looked @ couple of php cheat sheets , don't see immediate issues in syntax.

any pointers appreciated, thanks.

you need delimiters around regex, /s:

if (preg_match("/^[a-z0-9\-]{5}$/", $_get['serial']) === false)                 ^---------------^ 

but non-alphanumeric character valid (even paired brackets), although makes sense use ~, # or other symbols aren't regex metacharacters or used in text searches:

if (preg_match("#^[a-z0-9\-]{5}$#", $_get['serial']) === false) 

in case, pointed out andy lester, regex engine thinks ^ supposed delimiter (possible, lose "start of string" anchor use in regex , have use \a instead):

if (preg_match("^\a[a-z0-9\-]{5}$^", $_get['serial']) === false) 

Comments

Popular posts from this blog

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

html - How to style widget with post count different than without post count -

url rewriting - How to redirect a http POST with urlrewritefilter -