2

边界一词有一些我无法理解的东西。

$input="157-XYZ";
preg_match("/[^\d+\-]\bRDS|xyz|ABC\b/i", $input, $output);

以上preg_match匹配 XYZ 中的$input.

但是,如果我将替代项放在括号内/[^\d+\-]\b(RDS|xyz|ABC)\b/i,它似乎不会返回任何内容。我不能在这里使用括号来检索结果$output[1]吗?

4

1 回答 1

1

这个正则表达式是错误的:

preg_match("/[^\d+\-]\bRDS|xyz|ABC\b/i", $input, $output);

由于:[^\d+\-]表示匹配所有内容,除了

  1. 一个数字
  2. 字面意思+
  3. 字面意思-

您可以使用:

preg_match("/^\d+\-\b(RDS|xyz|ABC)\b/i", $input, $output);
于 2013-11-15T11:15:44.557 回答