条件正则表达式与文字结尾,只有在条件的某些部分匹配时才匹配

时间:2013-10-02 18:13:43

标签: php regex

这是我的测试代码:

$strings=array(
'Low Minimum',
'Low 10 Piece Minimum',
'10 piece minimum',
'20 piece minimum',
'low 6 piece minimum',
'100 piece minimum',
'This item is offered with a 1000 piece minimum',
'Place your order now. This item has a 75 piece minimum',
'Place your order now. This item has a low 75 piece minimum',
);

foreach($strings as $string)
  {
     echo preg_replace('/(low)? ?(\b[0-9]{0,3}\b) (piece minimum)/i',' Low Minumum',$string);
     echo '<br>';
  }

这是输出:

Low Minimum
Low Minumum
Low Minumum
Low Minumum
Low Minumum
Low Minumum
This item is offered with a 1000 Low Minumum
Place your order now. This item has a Low Minumum
Place your order now. This item has a Low Minumum

这是我想要的输出:

Low Minimum
Low Minumum
Low Minumum
Low Minumum
Low Minumum
Low Minumum
This item is offered with a 1000 piece minimum
Place your order now. This item has a Low Minumum
Place your order now. This item has a Low Minumum

让我烦恼的是4位数字。 因此,“最小片段”只应匹配,如果它之前的某些部分也匹配。 我怎么写这个?

1 个答案:

答案 0 :(得分:1)

对于您想要的输出,您需要更改一个小错误。

而不是..

[0-9]{0,3}

应该是......

[0-9]{1,3}