Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用preg_replace访问regex中的子模式_Php_Regex_Php 5.5 - Fatal编程技术网

Php 使用preg_replace访问regex中的子模式

Php 使用preg_replace访问regex中的子模式,php,regex,php-5.5,Php,Regex,Php 5.5,我有以下正则表达式: (.*?\s*){2,} 它与以下字符串匹配: 求职信应包括: 我需要访问求职信应该包括,因此我尝试: <?php $textarea = '<div class="dotted-highlight">The cover letter should include: <div class="dotted-highlight"><ul>'; $replacing_to = '(<div class="

我有以下正则表达式:

(.*?\s*){2,}

它与以下字符串匹配:

求职信应包括:

我需要访问
求职信应该包括
,因此我尝试:

    <?php
    $textarea = '<div class="dotted-highlight">The cover letter should include: 
<div class="dotted-highlight"><ul>';
    $replacing_to = '(<div class="dotted-highlight">(.*?)\s*){2,}<ul>';
    $replacing_with = '$1<ul>';
    $textarea = preg_replace('#'.$replacing_to.'#', $replacing_with, $textarea);
    ?>

$replacement_with
添加了
而不是所需的文本:
求职信应包括:

因此,输出将是:

预期产出为:

求职信应包括:

小提琴:

正则表达式测试器


只需使用此选项并使用第二个捕获组:

((<div class="dotted-highlight">)([^<\n]*)\s?).*?\2.*?<ul>

(()([^
strip_tags($textarea,
@JonathanKuhn做不到字符串太大了,我只添加了我想说的一点,如何匹配子模式。在这种情况下,您的替代者可能需要使用
$2
,因为它是
(*)的第二组
。也不能使用$2。它是一个子模式,也尝试使用$1\1,输出$1是div,它是第一个组。$2应该是。*这是您想要的。它可以使用,但不是旧的,我添加了{2,}因为如果有2个或更多的模式,就应该进行替换,所以现在如果我删除第一个模式,它将与第二个模式匹配,并且不应该进行替换。@Adrian请尝试我更新的正则表达式。所有示例都已更新。