preg_match_all适用于片段,但不适用于整个网页

时间:2012-10-22 22:20:22

标签: regex preg-match-all

我有一个适用于小部分代码的正则表达式,但是当它在整个页面上运行时它只返回第一个匹配。为什么?

我的问题与preg_match backreference to find ending tag有关。但现在我有一个不同的问题。

来自以下代码的正则表达式

/\{(.*?)_start\}.*\{\1_stop\}/s

Preg_match_all

$loopPattern = "/\\$this->tokenStartDelimiter(.*?)_start\\$this->tokenStopDelimiter.*\\$this->tokenStartDelimiter\\1_stop\\$this->tokenStopDelimiter/s";
preg_match_all($loopPattern, $this->template, $matches);
foreach ($matches[0] as $match) {
    $this->template = str_replace($match, '', $this->template);
}

整页

<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <link rel="apple-touch-icon" href="apple-touch-icon.png">

    <title>Appointment-Plus</title>

    <!--<link rel="stylesheet" href="css/themes/default/jquery.mobile.theme-1.2.0.min.css"> -->
    <link rel="stylesheet" href="css/themes/apcv/APCV-Theme.min.css">
    <link rel="stylesheet" href="css/themes/default/jquery.mobile.structure-1.2.0.min.css">

    <script type="text/javascript" src="js/jquery-1.8.2.min.js"></script>
    <script type="text/javascript" src="js/jquery.mobile-1.2.0.min.js"></script>
    <script type="text/javascript" src="js/jquery.validate.min.js"></script>
    <script type="text/javascript" src="js/pages/all.js"></script>
    <link rel="stylesheet" href="css/mfe.css">
</head>

<body>
<div data-role="page" data-theme="d">
    <!-- Some icons by Yusuke Kamiyamane. -->
    <div data-id="head" data-role="header" data-position="fixed" data-theme="b" data-tap-toggle="false">
        <h1>DB Games</h1>
        <a href="#" id="headingsActionButton" class="ui-btn-right headingsActionButton hidden" data-role="button" data-inline="true" data-icon="check">Save</a>

    </div>

    <div id="mainContent" data-role="content">
    <!--    <div data-role="fieldcontain">
            <div style="width: 100%; height: 100px; position: relative; background-color: #fbfbfb; border: 1px solid #b8b8b8;">
                <img src="https://codiqa.com/static/images/v2/image.png" alt="image" style="position: absolute; top: 50%; left: 50%; margin-left: -16px; margin-top: -18px;">
            </div>
        </div> -->
        <div data-role="fieldcontain">
    <div data-role="collapsible-set" class="mfe_collapsibles" data-theme="c" data-inset="false">

        <div id="appointmentHeading" data-action-id="appointmentNext" data-action-text="Next" data-a data-role="collapsible" data-collapsed="true" data-collapsed-icon="arrow-r" data-expanded-icon="arrow-d" data-iconpos="right">
            <h3 class="collapsibleMainHeading">New Appointment</h3>
            <p>
                <script src="js/pages/newAppointment.js"></script>
<div data-role="collapsible-set" data-content-theme="c" class="appointmentMakeCollapsibleSet">
    <div data-role="collapsible" data-collapsed="false" data-collapsed-icon="arrow-r" data-expanded-icon="arrow-d">
        <h3>Appointment</h3>
        <p>
            <form id="appointmentPrimaryForm">

    <label for="c_id" class="select required" required>Location: </label>
    <select name="c_id" id="c_id" data-native-menu="true" class="select required" required>
        <option value="" data-placeholder="true">Choose One</option>

        <option value="261"
        selected>Mesa</option>
        <option value="1007"
        >Chandler</option>
    </select>



    {AppointmentStaffSelect_start}
    <label for="apptCreateEditStaff" class="select required" required>With: </label>
    <select name="apptCreateEditStaff" id="apptCreateEditStaff" data-native-menu="true" class="select required" required>
        <option value="" data-service="" data-placeholder="true">Choose One</option>

        <option data-service="{Services}" value="5490"
        >Brian</option>
        <option data-service="{Services}" value="4388"
        >Mckenna</option>
        <option data-service="{Services}" value="4387"
        >Katelyn</option>
        <option data-service="{Services}" value="783"
        >David</option>
        <option data-service="{Services}" value="4389"
        >Ryan</option>
        <option data-service="{Services}" value="4394"
        >Ryan - c</option>
        <option data-service="{Services}" value="784"
        >Tami</option>
        <option data-service="{Services}" value="4390"
        >Jake</option>
    </select>
    {AppointmentStaffSelect_stop}
    <p>{Test}</p>
{AppointmentTest_start}
<p></p>
{AppointmentTest_stop}

    <label for="service_id" class="select required">Service: </label>
    <select name="service_id" id="service_id" data-native-menu="true" class="select required" required>
        <option value="" data-placeholder="true" data-duration="0" data-cost="0.00">Choose One</option>
        <option value="1174|30 Minute Gaming Pass" class="{Classes}" data-cost="3.00" data-duration="30">30 Minute Gaming Pass</option>
        <option value="1176|1 Hour Gaming Pass" class="{Classes}" data-cost="5.00" data-duration="60">1 Hour Gaming Pass</option>
        <option value="1177|2 Hour Gaming Pass" class="{Classes}" data-cost="7.00" data-duration="120">2 Hour Gaming Pass</option>
    </select>

    {AppointmentAddonService_start}
    <fieldset data-role="controlgroup" id="apptCreateEditAddonServicesFieldset">
        <legend>Add-on Service(s):</legend>

        <input type="checkbox" name="aos-5059" id="aos-5059" data-duration="30" data-cost="10.00" class="custom {Classes}"   />
        <label for="aos-5059" class="{Classes}">Unlimited Snack Bar</label>

        <input type="checkbox" name="aos-5060" id="aos-5060" data-duration="30" data-cost="5.00" class="custom {Classes}"   />
        <label for="aos-5060" class="{Classes}">Unlimited Energy Drinks</label>

        <input type="checkbox" name="aos-5061" id="aos-5061" data-duration="30" data-cost="1.00" class="custom {Classes}"   />
        <label for="aos-5061" class="{Classes}">Candy Bar</label>

    </fieldset>
    {AppointmentAddonService_stop}

    <label for="apptCreateEditDate" class="select required" required>Date: </label>
    <select name="apptCreateEditDate" id="apptCreateEditDate" data-native-menu="true" class="select required" required>
        <option value="" data-placeholder="true">Choose One</option>
        {apptCreateEditDateOption_start}
        <option value="{Value}"
        {Selected}>{Name}</option>{apptCreateEditDateOption_stop}
    </select>

    <label for="apptCreateEditTime" class="select required" required>Time: </label>
    <select name="apptCreateEditTime" id="apptCreateEditTime" data-native-menu="true" class="select required" required>
        <option value="" data-placeholder="true">Choose One</option>
        {apptCreateEditTimeOption_start}
        <option value="{Value}"
        {Selected}>{Name}</option>{apptCreateEditTimeOption_stop}
    </select>
</form>
        </p>
    </div>
    <div data-role="collapsible" data-collapsed="true" class="ui-disabled" data-collapsed-icon="arrow-r" data-expanded-icon="arrow-d">
        <h3>Children</h3>
        <p>
            Page 2
        </p>
    </div>
    <div data-role="collapsible" data-collapsed="true" class="ui-disabled" data-collapsed-icon="arrow-r" data-expanded-icon="arrow-d">
        <h3>Appointment Details</h3>
        <p>



<label for="customer_notes">Special Instructions for Customer: </label>
<textarea cols="40" rows="8" name="customer_notes" id="customer_notes"></textarea>


{apptCreateEditPONumber_start}
<label for="apptCreateEditPONumber" class="{Class}">{LName}: </label>
<input type="text" id="apptCreateEditPONumber" name="apptCreateEditPONumber" class="{Class}" value=""/>
{apptCreateEditPONumber_stop}

{apptCreateEditVehicleMake_start}
<label for="apptCreateEditMakeId" class="select {Class}">{LName}: </label>
<select name="apptCreateEditMakeId" id="apptCreateEditMakeId" data-native-menu="true" class="select {Class}">
    <option value="" data-placeholder="true">Choose One</option>
    {apptCreateEditMakeOption_start}
    <option value="{Value}"
    {Selected}>{Name}</option>{apptCreateEditMakeOption_stop}
</select>
{apptCreateEditVehicleMake_stop}

{apptCreateEditVehicleModel_start}
<label for="apptCreateEditModelId" class="select {Class}">{LName}: </label>
<select name="apptCreateEditModelId" id="apptCreateEditModelId" data-native-menu="true" class="select {Class}">
    <option value="" data-placeholder="true">Choose One</option>
    {apptCreateEditModelOption_start}
    <option value="{Value}" class="{Make}"
    {Selected}>{Name}</option>{apptCreateEditModelOption_stop}
</select>
{apptCreateEditVehicleModel_stop}

{apptCreateEditVehicleYear_start}
<label for="apptCreateEditModelYear" class="{Class}">{LName}: </label>
<select name="apptCreateEditModelYear" id="apptCreateEditModelYear" data-native-menu="true" class="select {Class}">
    <option value="" data-placeholder="true">Choose One</option>
    {apptCreateEditYearOption_start}
    <option value="{Value}"
    {Selected}>{Name}</option>{apptCreateEditYearOption_stop}
</select>
{apptCreateEditVehicleYear_stop}

{apptCreateEditVehicleOther_start}
<label for="apptCreateEditOtherVehicle" class="{Class}">{LName}: </label>
<input type="text" id="apptCreateEditOtherVehicle" name="apptCreateEditOtherVehicle" class="{Class}" value="{ApptCreateEditVehicleOther}"/>
{apptCreateEditVehicleOther_stop}

{apptCreateEditVehicleVIN_start}
<label for="apptCreateEditVIN" class="{Class}">{LName}: </label>
<input type="text" id="apptCreateEditVIN" name="apptCreateEditVIN" class="{Class}" value="{ApptCreateEditVehicleVIN}"/>
{apptCreateEditVehicleVIN_stop}

{apptCreateEditVehicleOdometer_start}
<label for="apptCreateEditOdometer" class="{Class}">{LName}: </label>
<input type="number" id="apptCreateEditOdometer" name="apptCreateEditOdometer" class="{Class}" value="{ApptCreateEditVehicleOdometer}"/>
{apptCreateEditVehicleOdometer_stop}
        </p>
    </div>

    <div data-role="collapsible" class="postRegistrationCollapsible ui-disabled" data-collapsed="true" data-collapsed-icon="arrow-r" data-expanded-icon="arrow-d">
        <h3>Register / Login</h3>
        <p>
            <script src="js/pages/postRegistration.js"></script>
<div data-role="fieldcontain" class="hidden">
    <p id="postRegistrationMessage"></p>
</div>
<a href="#registerPopupDiv" class="postRegistrationButtons" data-role="button" data-rel="popup" data-inline="true">Register</a>
<a href="#loginPopupDiv" class="postRegistrationButtons" data-role="button" data-rel="popup" data-inline="true">Login</a>
<div id="registerPopupDiv" data-role="popup" data-overlay-theme="a" class="ui-content" data-corners="true" data-shadow="true" data-position-to="window" data-transition="flow" data-theme="c">
    <form id="registerFormPopup" data-ajax="false">
    <div data-role="fieldcontain" data-inset="false">

<label for="first_name" class="required">First Name1:</label>
<input type="text" id="first_name" name="first_name" value="{Value}" class="required" required/>

<label for="middle_name" class="{Class}">Middle Name:</label>
<input type="text" id="middle_name" name="middle_name" value="{Value}" class="{Class}" {Required}/>

<label for="last_name" class="required">Last Name1:</label>
<input type="text" id="last_name" name="last_name" value="{Value}" class="required" required/>

<label for="email" class="{Class}">eMail:</label>
<input type="email" id="email" name="email" value="{Value}" class="{Class}" {Required}/>

<label for="login" class="{Class}">Login:</label>
<input type="text" id="login" name="login" value="{Value}" class="{Class}" {Required}/>

<label for="password" class="{Class}">Password:</label>
<input type="password" id="password" name="password" value="{Value}" class="{Class}" {Required}/>

<label for="customer_id_unique">Customer ID:</label>
<input type="text" id="customer_id_unique" name="customer_id_unique" value="{Value}" class="readonly" readonly/>

    </div>
    <div data-role="fieldcontain" data-inset="false">
        <input type="button" name="registerPopupButton" id="registerPopupButton" value="Register"/>
    </div>
</form>
</div>
<div id="loginPopupDiv" data-overlay-theme="a" data-role="popup" class="ui-content" data-corners="true" data-shadow="true" data-position-to="window" data-transition="flow" data-theme="c">
    <form id="loginFormPopup" data-ajax="false">
    <div data-role="fieldcontain" class="ui-hide-label">
        <label for="login-username">Username:</label>
        <input type="text" name="login-username" id="login-username" value="" placeholder="Username"/>

    </div>
    <div data-role="fieldcontain" class="ui-hide-label">

        <label for="login-password">Password</label>
        <input type="password" name="login-password" id="login-password" value="" placeholder="Password"/>
    </div>
    <div data-role="fieldcontain" data-inset="false">
        <input type="button" name="loginPopupButton" id="loginPopupButton" value="Login"/>
    </div>
    <div data-role="fieldcontain" data-inset="false">
        <a href="#" id="forgotPopupPassword" data-rel="popup">Forgot Password?</a>
    </div>
</form>
</div>
        </p>
    </div>

    <div data-role="collapsible" data-collapsed="true" class="ui-disabled" data-collapsed-icon="arrow-r" data-expanded-icon="arrow-d">
        <h3>Verification</h3>
        <p>
            Page 5
        </p>
    </div>
</div>
            </p>
        </div>

        {RegisterSection_start}
        <div id="registerHeading" class="preRegistration" data-action-id="register" data-action-text="Register" data-role="collapsible" data-collapsed="true" data-collapsed-icon="arrow-r" data-expanded-icon="arrow-d" data-iconpos="right">
            <h3 class="collapsibleMainHeading">Register</h3>
            <p>
                <script src="js/pages/register.js"></script>
<form id="registerForm" data-ajax="false">
    <div data-role="fieldcontain" data-inset="false">

<label for="first_name" class="required">First Name1:</label>
<input type="text" id="first_name" name="first_name" value="{Value}" class="required" required/>

<label for="middle_name" class="{Class}">Middle Name:</label>
<input type="text" id="middle_name" name="middle_name" value="{Value}" class="{Class}" {Required}/>

<label for="last_name" class="required">Last Name1:</label>
<input type="text" id="last_name" name="last_name" value="{Value}" class="required" required/>

<label for="email" class="{Class}">eMail:</label>
<input type="email" id="email" name="email" value="{Value}" class="{Class}" {Required}/>

<label for="login" class="{Class}">Login:</label>
<input type="text" id="login" name="login" value="{Value}" class="{Class}" {Required}/>

<label for="password" class="{Class}">Password:</label>
<input type="password" id="password" name="password" value="{Value}" class="{Class}" {Required}/>

<label for="customer_id_unique">Customer ID:</label>
<input type="text" id="customer_id_unique" name="customer_id_unique" value="{Value}" class="readonly" readonly/>

    </div>
    <div data-role="fieldcontain" data-inset="false">
        <input type="button" name="register" id="register" value="Register"/>
    </div>
</form>
            </p>
        </div>
        {RegisterSection_stop}
        <div data-role="collapsible" class="preRegistration" data-collapsed="true" data-collapsed-icon="arrow-r" data-expanded-icon="arrow-d" data-iconpos="right">
            <h3 class="collapsibleMainHeading">Login</h3>
            <p>
                <script src="js/pages/login.js"></script>
<form id="loginForm" data-ajax="false">
    <div data-role="fieldcontain" class="ui-hide-label">
        <label for="login-username">Username:</label>
        <input type="text" name="login-username" id="login-username" value="" placeholder="Username"/>

    </div>
    <div data-role="fieldcontain" class="ui-hide-label">

        <label for="login-password">Password</label>
        <input type="password" name="login-password" id="login-password" value="" placeholder="Password"/>
    </div>
    <div data-role="fieldcontain" data-inset="false">
        <input type="submit" name="loginSubmit" id="loginSubmit" value="Login"/>
    </div>
    <div data-role="fieldcontain" data-inset="false">
        <a href="#passwordResetDiv" data-rel="popup">Forgot Password?</a>
    </div>
</form>
<script src="js/pages/passwordReset.js"></script>

<div id="passwordResetDiv" data-overlay-theme="a" data-role="popup" class="ui-content" data-corners="true" data-shadow="true" data-position-to="window" data-transition="flow" data-theme="c">
    <form id="passwordResetForm">
        <div data-role="fieldcontain" class="ui-hide-label">
            <label for="email">Email:</label>
            <input type="text" name="email" id="email" value="" placeholder="Email" class="required" required/>
        </div>
        <div data-role="fieldcontain" data-inset="false">
            <input type="submit" name="submit" id="submit" value="Reset Password"/>
        </div>
    </form>
</div>

            </p>
        </div>

    </div>
</div>
    </div>

</div>
</body>
</html>

整页结果

Array
(
    [0] => Array
        (
            [0] => {AppointmentStaffSelect_start}
    <label for="apptCreateEditStaff" class="select required" required>With: </label>
    <select name="apptCreateEditStaff" id="apptCreateEditStaff" data-native-menu="true" class="select required" required>
        <option value="" data-service="" data-placeholder="true">Choose One</option>

        <option data-service="{Services}" value="5490"
        >Brian</option>
        <option data-service="{Services}" value="4388"
        >Mckenna</option>
        <option data-service="{Services}" value="4387"
        >Katelyn</option>
        <option data-service="{Services}" value="783"
        >David</option>
        <option data-service="{Services}" value="4389"
        >Ryan</option>
        <option data-service="{Services}" value="4394"
        >Ryan - c</option>
        <option data-service="{Services}" value="784"
        >Tami</option>
        <option data-service="{Services}" value="4390"
        >Jake</option>
    </select>
    {AppointmentStaffSelect_stop}
        )

    [1] => Array
        (
            [0] => AppointmentStaffSelect
        )

)

处理不错的小部分

{AppointmentStaffSelect_start}
    <label for="apptCreateEditStaff" class="select required" required>With: </label>
    <select name="apptCreateEditStaff" id="apptCreateEditStaff" data-native-menu="true" class="select required" required>
        <option value="" data-service="" data-placeholder="true">Choose One</option>

        <option data-service="{Services}" value="5490"
        >Brian</option>
        <option data-service="{Services}" value="4388"
        >Mckenna</option>
        <option data-service="{Services}" value="4387"
        >Katelyn</option>
        <option data-service="{Services}" value="783"
        >David</option>
        <option data-service="{Services}" value="4389"
        >Ryan</option>
        <option data-service="{Services}" value="4394"
        >Ryan - c</option>
        <option data-service="{Services}" value="784"
        >Tami</option>
        <option data-service="{Services}" value="4390"
        >Jake</option>
    </select>
    {AppointmentStaffSelect_stop}
    <p>{Test}</p>
{AppointmentTest_start}
<p></p>
{AppointmentTest_stop}

    <label for="service_id" class="select required">Service: </label>
    <select name="service_id" id="service_id" data-native-menu="true" class="select required" required>
        <option value="" data-placeholder="true" data-duration="0" data-cost="0.00">Choose One</option>
        <option value="1174|30 Minute Gaming Pass" class="{Classes}" data-cost="3.00" data-duration="30">30 Minute Gaming Pass</option>
        <option value="1176|1 Hour Gaming Pass" class="{Classes}" data-cost="5.00" data-duration="60">1 Hour Gaming Pass</option>
        <option value="1177|2 Hour Gaming Pass" class="{Classes}" data-cost="7.00" data-duration="120">2 Hour Gaming Pass</option>
    </select>

    {AppointmentAddonService_start}
    <fieldset data-role="controlgroup" id="apptCreateEditAddonServicesFieldset">
        <legend>Add-on Service(s):</legend>

        <input type="checkbox" name="aos-5059" id="aos-5059" data-duration="30" data-cost="10.00" class="custom {Classes}"   />
        <label for="aos-5059" class="{Classes}">Unlimited Snack Bar</label>

        <input type="checkbox" name="aos-5060" id="aos-5060" data-duration="30" data-cost="5.00" class="custom {Classes}"   />
        <label for="aos-5060" class="{Classes}">Unlimited Energy Drinks</label>

        <input type="checkbox" name="aos-5061" id="aos-5061" data-duration="30" data-cost="1.00" class="custom {Classes}"   />
        <label for="aos-5061" class="{Classes}">Candy Bar</label>

    </fieldset>
    {AppointmentAddonService_stop}

小部分页面的结果

Array
(
    [0] => Array
        (
            [0] => {AppointmentStaffSelect_start}
    <label for="apptCreateEditStaff" class="select required" required>With: </label>
    <select name="apptCreateEditStaff" id="apptCreateEditStaff" data-native-menu="true" class="select required" required>
        <option value="" data-service="" data-placeholder="true">Choose One</option>

        <option data-service="{Services}" value="5490"
        >Brian</option>
        <option data-service="{Services}" value="4388"
        >Mckenna</option>
        <option data-service="{Services}" value="4387"
        >Katelyn</option>
        <option data-service="{Services}" value="783"
        >David</option>
        <option data-service="{Services}" value="4389"
        >Ryan</option>
        <option data-service="{Services}" value="4394"
        >Ryan - c</option>
        <option data-service="{Services}" value="784"
        >Tami</option>
        <option data-service="{Services}" value="4390"
        >Jake</option>
    </select>
    {AppointmentStaffSelect_stop}
            [1] => {AppointmentTest_start}
<p></p>
{AppointmentTest_stop}
            [2] => {AppointmentAddonService_start}
    <fieldset data-role="controlgroup" id="apptCreateEditAddonServicesFieldset">
        <legend>Add-on Service(s):</legend>

        <input type="checkbox" name="aos-5059" id="aos-5059" data-duration="30" data-cost="10.00" class="custom {Classes}"   />
        <label for="aos-5059" class="{Classes}">Unlimited Snack Bar</label>

        <input type="checkbox" name="aos-5060" id="aos-5060" data-duration="30" data-cost="5.00" class="custom {Classes}"   />
        <label for="aos-5060" class="{Classes}">Unlimited Energy Drinks</label>

        <input type="checkbox" name="aos-5061" id="aos-5061" data-duration="30" data-cost="1.00" class="custom {Classes}"   />
        <label for="aos-5061" class="{Classes}">Candy Bar</label>

    </fieldset>
    {AppointmentAddonService_stop}
        )

    [1] => Array
        (
            [0] => AppointmentStaffSelect
            [1] => AppointmentTest
            [2] => AppointmentAddonService
        )

)

如何让它解析整个页面并返回所有匹配项?小部分解析正常,但是当我尝试搜索整个页面时,它似乎只在第一个{Test}令牌之前返回结果。

1 个答案:

答案 0 :(得分:2)

你要改变的第一件事就是让第二次重复也不合适:

/\{(.*?)_start\}.*?\{\1_stop\}/s

现在这不是什么大问题,因为你从来没有两次相同的标签。但是,如果你有,你将匹配从第一个开头到最后一个结束标签。这也应该(在大多数情况下)略微提高性能。

现在我只能假设你为什么只得到第一场比赛的原因是,正则表达式引擎只需要花费太长时间来回溯你的两次重复。

如果是{Test},这大致会发生这种情况:对于回溯(.*?)的每一步都会消耗一个字符。首先是T,然后是Te,然后是Tes,然后是Test,然后是Test},依此类推。如果其中任何一个匹配后跟一个下划线,它将在重复后继续。现在您已经可以看到,.匹配任何字符(包括换行符,因为s修饰符)。所以这个东西会为你的其余部分中的每一个字符尝试一次(尽管如此,你知道你只想看到下一个结束}。但是后来情况变得更糟。一旦(.*?)消耗了从Test}...{AppointmentTest的所有内容,它实际上能够匹配_start}。现在它会假设找到了匹配项,标记名称为Test}</p>\n{AppointmentTest并且将通过文件的其余部分(一次一个字符)执行相同的回溯,从不为此找到结束标记。这特别昂贵,因为引擎每次遇到{{1}时都必须访问捕获的组完成后,它将继续回溯第一个,重复,直到它认为它找到了另一个标记,从{开始到Test}...结束。这种情况发生在其他所有其他标记上。在代码中打开标签。我可以想象这很容易导致正则表达式引擎中止搜索。您可以查看preg_last_error是否确实如此。

现在该如何避免这种情况。让发动机提前停止回溯。也就是说,减少重复的可能性。例如,您知道您的代码永远不会包含...{AppointmentAddonService,因为这就是界定其结尾的内容。因此,如果你只是说,标签名称可以包含除}之外的任何字符,引擎最多将一直持续到标签结束(而不是整个文件),同时永远不能匹配标记从一个开头}到不相关的{。您可以使用否定的字符类来执行此操作。这也意味着您可以从第一次重复中删除不合格的_start}(因为它无论如何都不会超出开始标记的末尾):

?

第二次重复仍然会回溯整个文件,但这并不是真的可以避免,因为毕竟你的整个文件可能包含在你的两个标签中。但是,这种回溯最多只执行一次。 永远不会,因为/\{([^}]*)_start\}.*?\{\1_stop\}/s 之类的标记不会以{Test}结尾。

另请注意,如果您在标记名称中禁用下划线(因此永远不会出现_start之类的内容),您也可以在否定字符类中包含下划线,但这是一个相当小的优化。 / p>

无论如何,我希望这能解决您的问题(如果没有,请告诉我{Custom_section_start}返回的内容)。

你能从中得到什么?在使用preg_last_error之前要非常小心 - 特别是如果它应该匹配某些分隔的内容。您可以阅读here或通常通过该网站上的教程进行阅读。写得非常好告诉你正则表达式引擎的内幕是什么。