Home  >  Article  >  Backend Development  >  怎么编写不匹配指定单词的正则表达式

怎么编写不匹配指定单词的正则表达式

WBOY
WBOYOriginal
2016-06-13 12:54:271296browse

如何编写不匹配指定单词的正则表达式?
不如说一个字符串里面有"abc def ghi abcdef" 这里指定不匹配abc这个单词其他的"def ghi abcdef"都可以匹配我写的/\b(?!abc)\w+\b/。。只不过 这个有个问题的是以abc开头的单词都不会匹配。。这个正则表达式应该怎么写呢?


------解决方案--------------------
echo  preg_replace('/\babc\b/','',"abc def ghi abcdef");
------解决方案--------------------
多写一步,你再以空格分割就是了。
------解决方案--------------------
引用:
echo  preg_replace('/\babc\b/','',"abc def ghi abcdef");


不用那么麻烦的~


$str = 'abc def ghi abcdef';
preg_match_all('/\b(?!abc\b)\w+\b/', $str, $matches);
var_dump($matches);
?>


输出结果:
array(1) {
  [0]=>
  array(3) {
    [0]=>
    string(3) "def"
    [1]=>
    string(3) "ghi"
    [2]=>
    string(6) "abcdef"
  }
}
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn