如何跳过第一个正则表达式匹配?

发布于 2024-09-02 00:58:53 字数 375 浏览 7 评论 0原文

使用正则表达式和 php.ini 时是否有跳过第一个匹配项?

或者是否有某种方法可以使用 str_replace 来实现此目的。

谢谢

更新 我试图从另一个字符串中删除一个字符串的所有实例,但我想保留第一次出现的情况,例如

$toRemove = 'test';
$string = 'This is a test string to test to removing the word test';

输出字符串将是:

这是一个要test的测试字符串删除单词 test

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
I am trying to remove all the instances of a string from another string but I want to retain the first occurance e.g

$toRemove = 'test';
$string = 'This is a test string to test to removing the word test';

Ouput string would be:

This is a test string to test to removing the word test

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

可爱咩 2024-09-09 00:58:53
preg_replace('/((?:^.*?\btest\b)?.*?)\btest\b/', '$1', $string);

这个想法是匹配并捕获每个匹配之前的任何内容,然后将其重新插入。(?:^.*?test)? 导致第一个 em> 要包含在捕获中的 test 实例。 (所有 \b 都是为了避免部分单词匹配,例如 smartesttestify 中的 test。 )

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.)

何其悲哀 2024-09-09 00:58:53

简单的 PHP 方法:

<?php
    $pattern = "/an/i";
    $text = "banANA";
    preg_match($pattern, $text, $matches, PREG_OFFSET_CAPTURE);
    preg_match($pattern, $text, $matches, 0, $matches[0][1]);
    echo $matches[0];
?>

会给你“AN”。

更新:不知道这是替换。试试这个:

<?php
    $toRemove = 'test';
    $string = 'This is a test string to test to removing the word test';
    preg_match("/$toRemove/", $string, $matches, PREG_OFFSET_CAPTURE);
    $newString = preg_replace("/$toRemove/", "", $string);
    $newString = substr_replace($newString, $matches[0][0], $matches[0][1], 0);
    echo $newString;
?>

找到第一个匹配项并记住它在哪里,然后删除所有内容,然后将第一个位置的所有内容放回原位。

Easy PHP way:

<?php
    $pattern = "/an/i";
    $text = "banANA";
    preg_match($pattern, $text, $matches, PREG_OFFSET_CAPTURE);
    preg_match($pattern, $text, $matches, 0, $matches[0][1]);
    echo $matches[0];
?>

will give you "AN".

UPDATE: Didn't know it was a replace. Try this:

<?php
    $toRemove = 'test';
    $string = 'This is a test string to test to removing the word test';
    preg_match("/$toRemove/", $string, $matches, PREG_OFFSET_CAPTURE);
    $newString = preg_replace("/$toRemove/", "", $string);
    $newString = substr_replace($newString, $matches[0][0], $matches[0][1], 0);
    echo $newString;
?>

Find the first match and remember where it was, then delete everything, then put whatever was in the first spot back in.

↙温凉少女 2024-09-09 00:58:53

假设“blah”是您的正则表达式模式,blah(blah) 将匹配并捕获第二个模式

assume 'blah' is your regex pattern, blah(blah) will match and capture the second one

心舞飞扬 2024-09-09 00:58:53

答案迟到了,但可能对人们有用。

$string = "This is a test string to test something with the word test and replacing test";
$replace = "test";
$tmp = explode($replace, $string);
$tmp[0] .= $replace;
$newString = implode('', $tmp);
echo $newString; // Output: This is a test string to something with the word and replacing 

Late answer but it might be usefull to people.

$string = "This is a test string to test something with the word test and replacing test";
$replace = "test";
$tmp = explode($replace, $string);
$tmp[0] .= $replace;
$newString = implode('', $tmp);
echo $newString; // Output: This is a test string to something with the word and replacing 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文