[英]php regular expression for white space
i would like to validate user inputs like this: "Ahmedabad Intl-Ahmedabad, India(AMD)", The first letter is capital and there can be space and - in the first part. 我想验证用户输入,例如:“ Ahmedabad Intl-Ahmedabad,India(AMD)”,第一个字母为大写字母,并且可以有空格,并且-在第一部分中。 and then one comma with one space, and then some letters with '(' letters ')'.
然后是一个逗号和一个空格,然后是一些带有'('字母')'的字母。 I have tried it like this:
我已经这样尝试过了:
preg_match('/^([a-zA-Z- ]+), ([a-zA-Z]+)([A-Z()]+)/', $string),
Does anyone know why it does not work? 有谁知道为什么它不起作用? thanks
谢谢
<?php
$foo = "Dave Smith";
$bar = "SamSpade";
$baz = "Dave\t\t\tSmith";
var_dump(preg_match('/\s/',$foo));
var_dump(preg_match('/\s/',$bar));
var_dump(preg_match('/\s/',$baz));
Output 输出量
int(1)
int(0)
int(1)
see: https://stackoverflow.com/questions/1161708/php-detect-whitespace-between-strings 参见: https : //stackoverflow.com/questions/1161708/php-detect-whitespace-between-strings
You need to add backslash to the parenthesis. 您需要在括号中添加反斜杠。 Try this:
尝试这个:
$string = "Ahmedabad Intl-Ahmedabad, India(AMD)";
echo preg_match('/^[a-zA-Z- ]+, [a-zA-Z]+\([A-Z]+\)/', $string); //1
$string = "Ahmedabad Intl-Ahmedabad, India(AMD";
echo preg_match('/^[a-zA-Z- ]+, [a-zA-Z]+\([A-Z]+\)/', $string); //0
You can use this: 您可以使用此:
if (preg_match('~^[A-Z][a-z]*+(?>[ -][A-Z][a-z]*)*+, [A-Z][a-z]*\([A-Z]+\)$~', $string)) {
// true
} else {
// false
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.