English 中文(简体)
Regex 匹配字母数字字符、 下划线、 时段和破折符, 允许点与破折符只在中间
原标题:Regex to match alphanumeric characters, underscore, periods and dash, allowing dot and dash only in the middle

目前,我用的是这个:

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
  4. product.-name
  5. ____pro.-_-.d___uct.nam._-e

但我不想匹配:

  1. pro..ductname
  2. .productname-
  3. -productname.
  4. -productname
最佳回答

答案是

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

只允许包含 .- -. 的字符串不匹配。 为什么允许它们匹配呢? 但如果您真的需要这些字符串匹配, 一个可能的解决方案就是

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

第一个regex 的单 < code>. 或 < code> 或 < code> 的单个 < code > - < /code > 或 < code > 替换为交替顺序 . 和 < code> 。 从 < code > 开始, < code > 或 < code >, < code > 或 < code > 。 < /code > 或 < code >, 由顺序替换为顺序 < code > 。 < code > 和 < code > 替换为顺序,允许偶数的交替字符数。 复杂性可能是超标, 但根据当前规范看来是需要的。 如果需要最多2个交替 < code>. 和 < code >- , 则regex 变成regex 。

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

测试 < a href=\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

问题回答

< streng> 尝试此

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

<强 > UPATE 1

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

<强 > UPDATE 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) «$»
-->

您可以测试它 < a href=> "http://regexr.com?312r5" rel="no follow">这里 。

这应该做到:

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

It will make sure that the word begins and ends with alphanumeric or underscore character. The bracket in the middle will make sure that there will be at most one period or dash in a row, followed by at least one alphanumeric or underscore character.

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

这样可以确保第一个字符和最后一个字符不是破折或时段; 其余在中间的字符可以是任何字符( 在您所选择的一组内)。

下面的正则将检查含有字符、数字、破折号等字符串的任何字符串, 中间只有一个点 。

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

希望这能帮助





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签