Regex 需要正确的正则表达式范围来查找'@';在所附代码中

Regex 需要正确的正则表达式范围来查找'@';在所附代码中,regex,Regex,我需要正确的正则表达式范围才能在以下代码中找到@的所有实例: $TheString['Template1'] = '<h2> <strong> <strong> @@textarea_HEADLINE@@ </strong> </strong> </h2> <h4> <strong> @@text

我需要正确的正则表达式范围才能在以下代码中找到@的所有实例:

 $TheString['Template1'] = '<h2>
    <strong>
        <strong>
            @@textarea_HEADLINE@@
        </strong>
    </strong>
</h2>
<h4>    
    <strong>
        @@textarea_SUMMARY@@
    </strong>
</h4>
<p>
    @@textarea_DESCRIPTION@@
</p>
<div class="CB"  style="height:10px; clear:both;"></div>
<div id="sum_wrapper" style=" margin: 0 auto; float:left; height:auto; width:auto; ">
    <div id="sum_top" style="border-top: 1px solid #D8D8D8 ; width:auto; height:10px;"></div>
    <div id="Col1" style="width:196px; height:auto; float:left; ">
        <div style="word-wrap: break-word; padding:0 8px;">
            <strong>AVG user rating:</strong> <br> @@text_USERRATING@@ <br>&nbsp;
        </div>
        <div style="word-wrap: break-word; padding:0 8px;">
            <strong>How Much:</strong> <br>@@text_HOW-MUCH@@<br>&nbsp;
        </div>
        <div style="word-wrap: break-word; padding:0 8px;">
            <strong>File Size:</strong> <br>@@text_SIZE@@<br>&nbsp;
        </div>
    </div>
    <div id="Col2" style="width:198px; height:auto; float:left; border-left: 1px solid #D8D8D8;">
        <div style="word-wrap: break-word; padding:0 8px;">
            <strong>Visit:</strong> <br>@@text_VISIT@@<a href="">Developer\'s Website</a><br>&nbsp;
        </div>
        <div style="word-wrap: break-word; padding:0 8px;">
            <strong>Version Date:</strong> <br> @@text_UPDATED@@ <br>&nbsp;
        </div>
        <div style="word-wrap: break-word; padding:0 8px;">
            <strong>Category:</strong> <br> @@text_CATEGORY@@ <br>&nbsp;
        </div>
    </div>
    <div id="Col3" style="width:198px; height:auto; float:left; border-left: 1px solid #D8D8D8;">
        <div style="word-wrap: break-word; padding:0 8px;">
            <strong>Languages:</strong><br> @@text_LANG@@ <br>&nbsp;
        </div>
        <div style="word-wrap: break-word; padding:0 8px;">
            &nbsp; <br>&nbsp;
        </div>
        <div style="word-wrap: break-word; padding:0 8px;">
            <strong>Works on:</strong> <br> @@text_PLATFORMS@@ <br>&nbsp;
        </div>
    </div>
    <div class="CB"  style="height:0px; clear:both;"></div>
    <div id="sum_bottom" style="border-bottom: 1px solid #D8D8D8 ; width:auto; height:10px;"></div>
</div>
    <div class="CB"  style="height:10px; clear:both;"></div>
<strong>Comment:</strong> @@textarea_COMMENT@@';

$TheString['Template2'] = '<h2>
        <div id="header">
<h1>
    @@text_HEADER@@
</h1>
</div>

<div id="nav">
 @@text_CITY-1@@<br>  

 @@text_CITY-2@@<br>  

 @@text_CITY-3@@<br>  
</div>

<div id="section"">
<h1>CITY of Choice</h1>
<p>
   @@textarea_CITY-SUMMARY@@ 
</p>
<p>
   @@textarea_CITY-REVIEW@@   
</p>
</div>

<div id="footer">
    Copyright ©   @@text_COPYRIGHT@@  
</div>
$textInbetween=getInbetweenStrings('@','@',$theFullTemplate)

它适用于$TheString['Template1']上的所有实例,但不适用于$TheString['Template2']上的所有实例

我非常感谢你的帮助


谢谢

理想情况下,您希望正则表达式是
/(@)[a-zA-Z0-9_-]+(@@)/i

@([^@]*)@

你可以试试这个。看演示


有什么错误?是我瞎了还是你的两个例子完全相同?你需要一个
php
reference标记。噢,该死,犯了个错误。第二个代码是这样的:@@text\u HEADER@@@text\u CITY-1@@@br>@@text\u CITY-2@@@br>@@text\u CITY-3@@@br>您的正则表达式不匹配
-
作为OP的oneThanks@M42,我已经更新了代码,现在更新了链接
function getInbetweenStrings($start, $end, $theFullTemplate){
    $matches = array();
    $regex = "/$start([a-zA-Z0-9_-]*)$end/";
    preg_match_all($regex, $theFullTemplate, $matches);
    return $matches[0];
}
<?php

//Desired regular expression: (@@)[a-z_]+(@@)

function getInbetweenStrings($start, $end, $theFullTemplate){
    $matches = array();
    $regex = "/(". $start .")[a-zA-Z0-9_-]+(". $end .")/";
    preg_match_all($regex, $theFullTemplate, $matches);
    return $matches[0];
}