我有这样的文字:
$string = '
And God<WH430> said<WH559>, Behold<WH2009>, I have given<WH5414> you every herb<WH6212>
bearing<WH2232> seed<WH2233>, which <FI>is<Fi> upon the face<WH6440> of all the
earth<WH776>, and every tree<WH6086>, in the which <FI>is<Fi> the fruit<WH6529> of a
tree<WH6086> yielding<WH2232> seed<WH2233>; to you it shall be<WH1961> for meat<WH402>.
<RF>bearing...: Heb. seeding seed<Rf><RF>yielding...: Heb. seeding seed<Rf>';
这段代码:
$search = '/<RF>(.*)<Rf>/';
$replace = ' <sup data-tooltip class="has-tip" title="$1"> note </sup>';
$string = preg_replace($search, $replace, $string);
我希望正则表达式单独替换每个标记,但是使用此代码我得到了这个:
And God<WH430> said<WH559>, Behold<WH2009>, I have given<WH5414> you every herb<WH6212>
bearing<WH2232> seed<WH2233>, which <FI>is<Fi> upon the face<WH6440> of all the
earth<WH776>, and every tree<WH6086>, in the which <FI>is<Fi> the fruit<WH6529> of a
tree<WH6086> yielding<WH2232> seed<WH2233>; to you it shall be<WH1961> for meat<WH402>.
<sup data-tooltip class="has-tip" title="bearing...: Heb. seeding seed<Rf>
<RF>yielding...: Heb. seeding seed"> note </sup>
所以,它只是跳过中间的标签,直到文本末尾的最后一个......我怎样才能得到......某个tekst的每个实例......单独替换?
答案 0 :(得分:0)
你的正则表达式太贪心了,所以.*
消耗尽可能多的文本。将其修改为:
/<RF>(.*?)<Rf>/
^
Added question mark
这将使正则表达式根据需要捕获 as 文本以匹配表达式。
答案 1 :(得分:0)
如果您确定自己的文字不包含<
,则可以执行以下操作:
$search = '~<RF>([^<]*+)<Rf>~';
否则你可以使用:
$search = '~<RF>((?>[^<]++|<(?!Rf>))*+)<Rf>~';