[英]Check if string does not have 6 digit number
I'm trying to check if a string does not have any 6 digit number. 我正在尝试检查字符串是否没有任何6位数字。
$myString = "https://www.website.net/sometext/123456/";
if ( preg_match( '/([^0-9]{6})/', $myString ) ) {
echo 'If';
} else {
echo 'Else';
}
The code above echos if
which it should be false. 上面的代码回显if
为假。 I am not sure what I am missing. 我不确定我缺少什么。
This what I'm trying to achieve: 这是我想要达到的目标:
"https://www.website.net/sometext/123456/" -> false
"https://www.website.net/sometext/" -> true
"https://www.website.net/" -> true
Either check whether the result of the preg_match
is 0, while using \\d{6}
: 使用\\d{6}
,请检查preg_match
的结果是否为0:
if ( preg_match( '/\d{6}/', $myString ) === 0) {
echo 'If';
} else {
echo 'Else';
}
Or, if you wanted the preg_match
to return 1
if the string doesn't contain any such number, repeat each character from the start to the end of the string while using negative lookahead for 6 characters: 或者,如果您希望preg_match
如果字符串不包含任何这样的数字,则返回1
,则从字符串的开头到结尾重复每个字符,同时对6个字符使用负前瞻:
if ( preg_match( '/^(?:.(?!\d{6}))+$/', $myString )) {
echo 'If';
} else {
echo 'Else';
}
Also note that [^0-9]
means "anything but a digit" - but, that can be matched just with the \\D
metacharacter instead. 还要注意, [^0-9]
意思是“除了数字以外的任何东西”,但是可以只与\\D
元字符匹配。 (similarly, to match a digit, or [0-9]
, use \\d
- don't use the character sets) (类似地,要匹配数字或[0-9]
,请使用\\d
请勿使用字符集)
Instead of trying to say "not this" in regex, express that in PHP: 与其尝试在正则表达式中说“ not this”,不如在PHP中表达它:
if ( preg_match( '/([0-9]{6})/', $myString ) !== 1) {
echo 'If';
} else {
echo 'Else';
}
Your current regex /([^0-9]{6})/
means "6 non-digit characters", not "does not contain 6 digits". 您当前的正则表达式/([^0-9]{6})/
意思是“ 6个非数字字符”,而不是“不包含6个数字”。
if (preg_match('/[0-9]\d{5}/',$myString)) {
echo 'If';
}else{
echo 'else';
}
Your expression matches any string with 6 consecutive non-digit chars. 您的表达式匹配具有6个连续的非数字字符的任何字符串。 You need to match string with 6 digits then inverse the result: 您需要将字符串与6位数字匹配,然后将结果取反:
if ( ! preg_match('/([0-9]{6})/', $myString)) {
...
}
You may match a 6 digit number with (?<!\\d)\\d{6}(?!\\d)
pattern. 您可以将6位数字与(?<!\\d)\\d{6}(?!\\d)
模式匹配。
You may use it in a preg_match
call and negate the result: 您可以在preg_match
调用中使用它并取反结果:
if (!preg_match('~(?<!\d)\d{6}(?!\d)~', "text1234567 more text")) {
echo "Does not contain a 6 digit number";
}
Or, you may add it to the negative lookahead and use preg_match
without negation: 或者,您可以将其添加到否定前瞻中,并使用preg_match
不加取反:
if (preg_match('~^(?!.*(?<!\d)\d{6}(?!\d))~s', "text1234567 more text")) {
echo "Does not contain a 6 digit number";
}
Pattern details 图案细节
^
- start of string ^
-字符串开头 (?!.*(?<!\\d)\\d{6}(?!\\d))
- a negative lookahead that fails the match if there is a match of (?!.*(?<!\\d)\\d{6}(?!\\d))
-如果匹配项为,则负前瞻将使匹配失败
.*
- any 0+ chars as many as possible ( s
modifier makes .
match any char) .*
-尽可能多的0个字符( s
修饰符使.
匹配任何字符) (?<!\\d)\\d{6}(?!\\d)
- 6 digits that are neither preceded nor followed with a digit. (?<!\\d)\\d{6}(?!\\d)
-6个数字,均不得在数字前或后。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.