我使用RegEx (php)查找哪个地址有一个或多个大写字母。例如:
av。圣琼128 (不匹配)
Av。圣琼122 (比赛)
av。圣琼212 (比赛)
Av。St.joan 121 (不匹配)
AV。圣琼232 (不匹配)
发布于 2017-07-08 15:09:06
我想你只需要模式而不是
php代码,否则请评论我删除答案。
一个简单的消极展望断言:
^(?!.*[A-Z].*[A-Z]).*$ 它不匹配任何有超过一个大写字母的整行。
正如你所说:
我需要一个regex来找出哪个地址有一个或更少,即没有大写字母。
它还匹配no大写字母
因此,它只是将行与匹配--只有一个大写字母,或者与匹配,而不是大写字母。
供投入:
av. St Joan 128 (Not maach) | more than one
Av. st joan 122 (match) | just one
av. St joan 212 (match) | just one
Av. St.joan 121 (Not match) | more than one
AV. st joan 232 (Not match) | more than one
av. tt joan 212 (match) | no one产出如下:
Av. st joan 122 (match) | just one
av. St joan 212 (match) | just one
av. tt joan 212 (match) | no one 注意:要反转匹配,只需将?!更改为?=:
^(?=.*[A-Z].*[A-Z]).*$ 它符合:
av. St Joan 128 (Not maach) | more than one
Av. St.joan 121 (Not match) | more than one
AV. st joan 232 (Not match) | more than one发布于 2017-07-08 13:49:42
$strings = [
'av. St Joan 128',
'Av. st joan 122',
'av. St joan 212',
'Av. St.joan 121',
'AV. st joan 232'
];
$results = preg_grep('~^[^A-Z]*[A-Z]?[^A-Z]*$~', $strings);模式很简单,描述了整个字符串(使用锚来显示字符串的开始和结束)。
细目:
^ #match start of the string
[^A-Z]* #match zero or more (greedily) non-uppercase characters
[A-Z]? #match zero or one uppercase character
[^A-Z]* #match zero or more (greedily) non-uppercase characters
$ #match end of the string意思是,字符串可能有无限的非大写字符,但大写字母只能出现0或一次。如果模式找到第二个大写字符(任意位置),则立即出现错误匹配/失败。
请注意,还可以在PREG_GREP_INVERT选项中使用@bobble泡模式来排除匹配的字符串:
$result = preg_grep('~[A-Z][^A-Z]*[A-Z]~', $strings, PREG_GREP_INVERT);发布于 2017-07-08 13:40:05
您希望使用PHP来查找所有发生的模式。
您可以使用PCRE将模式定义为任意大写字母,因此:
[A-Z]= and在A(索引65)和Z(索引90) (区分大小写)之间的单个字符
示例:
$upper = "string oF Occurances";
$count = preg_match_all("/[A-Z]/",$upper);
// $count shows the number of upper case letters found in string
// $count = 2
if ($count == 1){
print "string ".$upper." has one Capital letter.";
}我的工作假设您是基于每个字符串,而不是每一行,例如,您的示例文本是每一行中的五个字符串。
https://stackoverflow.com/questions/44986548
复制相似问题