I have the fol开发者_开发问答lowing string:
<a name="subhd_182"></a>
<a name="st_394"></a>
<a name="st_395"></a>
<a name="qn_494"></a>
<a name="st_495"></a>
<a name="qn_594"></a>
<a name="st_595"></a>
<a name="subhd_282"></a>
<a name="qn_694"></a>
<a name="st_695"></a>
<a name="qn_794"></a>
<a name="st_795"></a>
<a name="qn_894"></a>
<a name="st_895"></a>`
And I want to replace every <a name="st_\d*"></a>
with <a name="qn_\d*"></a>
if it follows immediately <a name="subhd_\d*"></a>
I use this regex %(.*<a name="subhd_.*)(?=<a name="st(?!<a name="qn))(<a name=")st(.*)%sU
and replace with $1$2qn$3
. But it also replaces second case too
I'm assuming you only want to match name after the first subhd row above, but not the second, since the first one is an "st_" and the second one is a "qn_".
Try:
(<a name="subhd_\d+">\s*<\/a>\s*<a name=")st(_\d+">)
where you would replace as $1qn$2
Note that here I have assumed that you were quite literal when you said "it follows immediately .
I don't really understand why you're throwing the lookahead in, unless the actual rule you're trying to implement is more complicated than you've stated.
Try: %(<a name="subhd_\d+"></a>\n<a name=")st(.*)%sU
and replace with $1qn$2
. On a sidenote I don't really know what the U modifier does for you here. Also, you might want to change your \n
newline matcher according to your operating system.
I have found RegExr a really useful tool for regular expressions.
精彩评论