开发者

Regular Expression change text between tags

开发者 https://www.devze.com 2023-04-09 08:55 出处:网络
I have some code in the following layout,i m using textcrawler to do a find and replace <a> Name=LineA

I have some code in the following layout,i m using textcrawler to do a find and replace

<a>
Name=LineA
epsium
ask
answer
line=10
color=red
</a>

<a>
Name=LineB
Color=Blue
</a>

...

Now the question is what regular expression i need to use so as to remove 开发者_运维知识库the second block of code between <a> and </a>


<a>(\s*?Name\=LineB[\S\s]*?)</a>

It captures all text between and including the <a></a> tags that starts with the text Name=LineB.


In Perl, I'll do :

$str =~ s~^(.*?<a>.*?</a>.*?)<a>.*?</a>(.*)$~${1}New text$2~s;

the first group contains everything before the second block <a></a> and the second group everything after.

In php:

$str = preg_replace('~^(.*?<a>.*?</a>.*?)<a>.*?</a>(.*)$~', "${1}New text$2", $str);


preg_replace("/<body>([\s\S]*.*)<\/body>/",$replace,$origional);

this will replace whole content between body tags.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号