The regular expression matches alphanumeric characters, underscores, periods, and dashes, allowing the dot and dash to only be in the middle

I am currently using this:

if (preg_match ('/^[a-zA-Z0-9_]+([a-zA-Z0-9_]*[.-]?[a-zA-Z0-9_]*)*[a-zA-Z0-9_]+$/', $product) ) {
return true;
} else { 
return false
}

For example, I want to combine:

  • pro.duct-name_
  • _pro.duct.name
  • p.r.o.d_u_c_t.n-a-m-e

But I do not want to match:

  • pro..ductname
  • .productname-
  • -productname.
  • -productname
+5
source share
5 answers

The answer will be

/^[a-zA-Z0-9_]+([-.][a-zA-Z0-9_]+)*$/

unless you have allowed strings containing .-and -.NOT for matching. Why would you let them fit, anyway? But if you really need these lines to match, maybe a solution

/^[a-zA-Z0-9_]+((\.(-\.)*-?|-(\.-)*\.?)[a-zA-Z0-9_]+)*$/

. - . -, . -, -. .- , , a - . , . , , , , -, . 2 . -,

/^[a-zA-Z0-9_]+((\.-?|-\.?)[a-zA-Z0-9_]+)*$/

+9

(?im)^([a-z_][\w\.\-]+)(?![\.\-])\b

1

(?im)^([a-z_](?:[\.\-]\w|\w)+(?![\.\-]))$

2

(?im)^([a-z_](?:\.\-\w|\-\.\w|\-\w|\.\w|\w)+)$

<!--
(?im)^([a-z_](?:\.\-\w|\-\.\w|\-\w|\.\w|\w)+)$

Match the remainder of the regex with the options: case insensitive (i); ^ and $ match at line breaks (m) «(?im)»
Assert position at the beginning of a line (at beginning of the string or after a line break character) «^»
Match the regular expression below and capture its match into backreference number 1 «([a-z_](?:\.\-\w|\-\.\w|\-\w|\.\w|\w)+)»
   Match a single character present in the list below «[a-z_]»
      A character in the range between "a" and "z" «a-z»
      The character "_" «_»
   Match the regular expression below «(?:\.\-\w|\-\.\w|\-\w|\.\w|\w)+»
      Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
      Match either the regular expression below (attempting the next alternative only if this one fails) «\.\-\w»
         Match the character "." literally «\.»
         Match the character "-" literally «\-»
         Match a single character that is a "word character" (letters, digits, and underscores) «\w»
      Or match regular expression number 2 below (attempting the next alternative only if this one fails) «\-\.\w»
         Match the character "-" literally «\-»
         Match the character "." literally «\.»
         Match a single character that is a "word character" (letters, digits, and underscores) «\w»
      Or match regular expression number 3 below (attempting the next alternative only if this one fails) «\-\w»
         Match the character "-" literally «\-»
         Match a single character that is a "word character" (letters, digits, and underscores) «\w»
      Or match regular expression number 4 below (attempting the next alternative only if this one fails) «\.\w»
         Match the character "." literally «\.»
         Match a single character that is a "word character" (letters, digits, and underscores) «\w»
      Or match regular expression number 5 below (the entire group fails if this one fails to match) «\w»
         Match a single character that is a "word character" (letters, digits, and underscores) «\w»
Assert position at the end of a line (at the end of the string or before a line break character) «$»
-->

.

+3

:

/^[A-z0-9_]([.-]?[A-Z0-9_]+)*[.-]?[A-z0-9_]$/

, - . , , , , - .

+1
/^[A-Z0-9_][A-Z0-9_.-]*[A-Z0-9_]$/i

, ; ( ).

0

, , , .., .

/^[A-Za-z0-9_-]+(\.){1}[A-Za-z0-9_-]+$/i

,

0

All Articles