【问题标题】:match all except some word匹配除某些单词之外的所有内容
【发布时间】:2010-08-27 14:00:13
【问题描述】:

我正在寻找一种方法,以匹配除某些单词之外的所有单词。

请告诉我应该怎么写?

我写的时候

$str = "i am a programmer";
$word = "am";
preg_match_all("/[^($word)]/", $str, $matched);// it do the same, if i when i wrote
preg_match_all("/[^$word]/", $str, $matched);

我也试过preg_match_all("/[^{$word}]/", $str, $matched);,但它不起作用。

我怎样才能告诉除了那个词之外的所有内容

非常感谢

【问题讨论】:

  • 您希望得到什么样的结果?一个字母数组还是一个没有单词的字符串?
  • @Simon 我可以在 JS 上显示,可以吗?
  • @Simon 用例子写出需要做什么。

标签: php regex


【解决方案1】:

你不能简单地删除所有出现的单词吗?

str_replace($word, '', $str);

还是使用explode() 或preg_split() 分割?这将为您提供一个由单词分隔的所有部分的数组。

【讨论】:

  • 我需要它在大正则表达式中使用它,所以我确实需要写像[^...] 这样的东西,因为我也需要提到not that word
  • 也许你想四处看看:regular-expressions.info/lookaround.html
猜你喜欢
  • 1970-01-01
  • 2017-11-25
  • 2019-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多