5

現在、私はこれを使用しています:

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
}

たとえば、一致させたい:

  1. pro.duct-name_
  2. _pro.duct.name
  3. p.r.o.d_u_c_t.n-a-m-e

しかし、私は一致したくありません:

  1. pro..ductname
  2. .productname-
  3. -productname.
  4. -productname
4

5 に答える 5

11

答えは

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

.-and -.NOT を含む文字列を許可した場合にのみ一致します。とにかく、なぜそれらを一致させるのですか?しかし、これらの文字列も一致させる必要がある場合、可能な解決策は次のとおりです。

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

最初の正規表現の単一の.またはは、交互のおよび-のシーケンスに置き換えられます。これは、 または のいずれかで始まり、オプションでまたはのペアがそれぞれ続き、オプションでまたはそれぞれが続き、偶数の交互の文字を可能にします。この複雑さはおそらく行き過ぎですが、現在の仕様では必要とされているようです。最大 2 つの交互が必要な場合、正規表現は次のようになります。.-.--..--..-

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

ここまたはここでテスト

于 2012-05-26T07:55:03.590 に答える
3

これを試して

(?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) «$»
-->

そして、ここでそれをテストすることができます。

于 2012-05-26T07:07:25.207 に答える
1

これは次のことを行う必要があります。

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

単語が英数字またはアンダースコア文字で始まり、終わることを確認します。中央のブラケットは、ピリオドまたはダッシュが 1 行に最大 1 つ存在し、その後に少なくとも 1 つの英数字またはアンダースコア文字が続くことを確認します。

于 2012-05-26T07:22:35.083 に答える
0

以下の正規表現は、文字、数字、ダッシュなどを含む文字列と、中央に 1 つのドットのみをチェックします。

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

お役に立てれば

于 2012-05-26T07:22:04.973 に答える
0
/^[A-Z0-9_][A-Z0-9_.-]*[A-Z0-9_]$/i

これにより、最初と最後の文字がダッシュやピリオドではないことが保証されます。その間の残りは、(選択したセット内の)任意の文字で構成できます。

于 2012-05-26T07:15:24.760 に答える