1

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

我的问题与preg_match 反向引用以查找结束标签有关。但现在我有一个不同的问题。

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

/\{(.*?)_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}标记之前返回结果。

4

1 回答 1

2

您应该更改的第一件事是使第二次重复也变得不贪心:

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

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

现在我只能假设你为什么只得到第一个匹配的原因是,正则表达式引擎在你的两次重复中都需要太长时间的回溯。

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

现在如何避免这种情况。让引擎提前停止回溯。也就是说,减少重复的可能性。例如,您知道您的标签永远不会包含一个}因为,这就是它们的结尾。因此,如果您简单地说,标签名称可以包含任何字符,除了}引擎将最多持续到标签末尾(而不是贯穿整个文件),同时永远无法匹配标签从一个开头{到一个无关的_start}。您可以使用否定字符类来执行此操作。这也意味着您可以从第一次重复中删除 ungreedy- ?(因为它永远不会超过开始标签的末尾):

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

第二次重复仍然会回溯整个文件,但这并不是真的可以避免的,因为毕竟你的整个文件可能被包装在你的两个标签中。但是,这种回溯最多只执行一次。并且永远不要像这样的标签{Test}不以_start.

另请注意,如果您在标签名称中不允许使用下划线(因此永远不会有类似的东西{Custom_section_start}),您还可以在否定字符类中包含下划线,但这是一个相当小的优化。

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

你能从中得到什么?使用前要非常小心.*- 特别是如果它应该匹配一些分隔的内容。您可以在此处阅读或通过该网站上的教程进行阅读。它写得很好告诉你正则表达式引擎的底层发生了什么。

于 2012-10-23T13:19:44.450 回答