语言PHP
想用preg_match_all来正则一个字符串中包含了多此次自定义的标签
例如标签里面有 : 你好、中国、国家、新年、娱乐、程序、羁绊、www.baidu.com
待处理的字符串:阿斯顿TV u有单反啊送分国家v晕车i哦弄请问娱乐哦怕麻烦NSA大佛你程序i哦就拿谁都i那都
然后就是判断字符串是否包含上面的标签,包含了多少个
我现在写出来的中文可以 但是像英文就不行
例如像标签里面的“www.baidu.com”
他不会去匹配字符串里面的 www.baidu.com 而是把 w、b、a、u、c等等这些字符匹配出来了
代码:
$pattaern0='/[你好|中国|国家|新年|娱乐|程序|羁绊|www.baidu.com|google]+/u';
if(preg_match_all($pattaern0,$str,$matches1))
{
var_dump($matches1[0])
}
[]改为()
$pattaern0='/(你好|中国|国家|新年|娱乐|程序|羁绊|www\\.baidu\\.com|google)+/u';
$pattaern0 = '/(\w+)www.(\w+).(com|org)/';
$str = "/[你好|中国|国家|新年|娱乐|程序|羁绊|www.baidu.com|google]+/u";
preg_match($pattaern0, $str, $match);
print_r($match);
$pattaern0 = '/(\w+)www.(\w+).(com|org)/';
$str = "/(你好|中国|国家|新年|娱乐|程序|羁绊|www.baidu.com|google)+/u";
preg_match($pattaern0, $str, $match);
print_r($match);
去掉中括号以及后面的+号即可
$pattaern0='/你好|中国|国家|新年|娱乐|程序|羁绊|www.baidu.com|google/u';
if(preg_match_all($pattaern0,$str,$matches1))
{
var_dump($matches1[0])
}
如果不去掉后面的+号,当出现:你好你好这种时,只能统计算1个,所以必须去+号
.也是需要转义的,不转义www+baidu+com这种也能匹配到,所以完整的应该这样
$pattaern0='/你好|中国|国家|新年|娱乐|程序|羁绊|www\.baidu\.com|google/u';
if(preg_match_all($pattaern0,$str,$matches1))
{
var_dump($matches1[0])
}
[]里面的内容只能按单字匹配,所以你看到其它人改为(),()则是按词计算
+则是重复连续出现算一个匹配,所以要去掉+
如果不区分大小写,最后应该加上i修饰 /你好|中国|国家|新年|娱乐|程序|羁绊|www.baidu.com|google/ui
var obj = {email:'name1.com',company_name:'echemi',name:'echemi123'}
var matcher = /n/i //console.log(matcher.test(obj)) false
// var matcher = /e/i //console.log(matcher.test(obj)) true
// var matcher = /c/i //console.log(matcher.test(obj)) true
// var matcher = /ec/i //console.log(matcher.test(obj)) true
console.log(matcher.test(obj))