PHP正则应用07 —— 匹配纯单词(纯数字、纯字母)

// 匹配纯单词

$str = 'baidu o2o b2b heol xiling shuai chou bage ss2';

$patt = '/\b[a-zA-Z]+\b/';

preg_match_all($patt,$str,$res);

var_dump($res);
/*
array (size=1)
  0 => 
    array (size=6)
      0 => string 'baidu' (length=5)
      1 => string 'heol' (length=4)
      2 => string 'xiling' (length=6)
      3 => string 'shuai' (length=5)
      4 => string 'chou' (length=4)
      5 => string 'bage' (length=4)
*/

// 查询纯数字或纯字母的词
注:正则中一个竖线代表或者

$str = 'hello o2o 2b9 250';

$patt = '/\b[a-zA-Z]+\b|\b[0-9]+\b/';

preg_match_all($patt,$str,$res);

print_r($res);
// Array ( [0] => Array ( [0] => hello [1] => 250 ) ) 

你可能感兴趣的:(#,——【,PHP,Regex,Application】,PHP正则基础应用)