How to skip first regex match?

后端 未结 4 2011
伪装坚强ぢ
伪装坚强ぢ 2020-12-22 01:55

Is there anyway to skip the first match when using regex and php.

Or is there some way of achieveing this using str_replace.

Thanks

UPDATE

4条回答
  •  太阳男子
    2020-12-22 02:03

    preg_replace('/((?:^.*?\btest\b)?.*?)\btest\b/', '$1', $string);
    

    The idea is to match and capture whatever precedes each match, and plug it back in. (?:^.*?test)? causes the first instance of test to be included in the capture. (All the \bs are to avoid partial-word matches, like the test in smartest or testify.)

提交回复
热议问题