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

WBOY
Release: 2016-06-13 11:13:52
Original
1770 people have browsed it

如何编写不匹配指定单词的正则表达式?
不如说一个字符串里面有"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");


不用那么麻烦的~

<?php <br />$str = 'abc def ghi abcdef';<br />preg_match_all('/\b(?!abc\b)\w+\b/', $str, $matches);<br />var_dump($matches);<br />?>
Copy after login


输出结果:
array(1) {
  [0]=>
  array(3) {
    [0]=>
    string(3) "def"
    [1]=>
    string(3) "ghi"
    [2]=>
    string(6) "abcdef"
  }
}
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template