我需要匹配一个字母数字的单词。 它可以包含“ - ”, 但不包含在结尾或开头, 并且“ - ” 不能重复( 如“ - ” ), 单词长度必须在 2 到 24 个字符之间 。
一些例子:
adfg
asd-asdasd
asd-asd-asd
这些都不应相匹配:
-asd
asd-
-asd-
-asd--asd-
我需要匹配一个字母数字的单词。 它可以包含“ - ”, 但不包含在结尾或开头, 并且“ - ” 不能重复( 如“ - ” ), 单词长度必须在 2 到 24 个字符之间 。
一些例子:
adfg
asd-asdasd
asd-asd-asd
这些都不应相匹配:
-asd
asd-
-asd-
-asd--asd-
使用regexes最困难的一件事是抵制过度聪明的诱惑。你有两个不同的、不相容的测试要进行。长度 :
/^.{2,24}$/
...和组成:
/^[A-Z0-9]+(?:-[A-Z0-9]+)*$/i
虽然 might 有可能提出一个正方形, 在一个关口进行两次测试, 但没有必要。 只要先看一个即可 :
/^(?=.{2,24}$)[A-Z0-9]+(?:-[A-Z0-9]+)*$/i
尝试此 PCRE Reg 演示文稿 :
/^(([0-9a-z]+-)*[0-9a-z]){2,24}$/i
Regular expressions depend on the platform you are using, and they are not very good for matching sizes (if you try to determine a specific length, you will probably loose flexibility, unless you are checking more standardized inputs, like dates: d{2}/d{2}/d{4})
.
But something like:
[a-z]+(-[a-z]+)*
可能给你一个想法 为你的问题。
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 ...
<?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 = ...
I found this script online that creates a thumbnail out of a image but the thumbnail image is created with poor quality how can I improve the quality of the image. And is there a better way to create ...
如何确认来自正确来源的数字。
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 ...
I wonder there is a way to post a message to a facebook business page with cURL? thanks
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? 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 ...