我想使用Regex查找HTML代码中的所有单词匹配,然后用链接替换它们,但我需要从搜索中排除具有特定类的标记。 在这个例子中,我需要找到所有单词:
"真棒"
并从搜索中排除所有带有类的标记:
goaway
最后用我的链接替换它们(只有内部标签,而不是标题或ALT属性):
<a href="mysite.com">awesome</a>
源HTML代码:
<p>
this is awesome text about my cat
<img src="cat.jpg" title="my awesome cat"/>
</p>
<p class="goaway">
I don't need this awesome match
</p>
<div>
and this is element of my awesome code
</div>
<span class="goaway">
That isn't awesome word
</span>
我正在使用preg_replace_callback()
函数进行替换,这就是我希望看到的结果:
<p>
this is <a href="mysite.com">awesome</a> text about my cat
<img src="cat.jpg" title="my awesome cat"/>
</p>
<p class="goaway">
I don't need this awesome match
</p>
<div>
and this is element of my <a href="mysite.com">awesome</a> code
</div>
<span class="goaway">
That isn't awesome word
</span>
现在,我所取得的一切都是忽略属性内容的模式
"|(?!<.*?)awesome(?![^<>]*?>)|"
所以,我需要你帮助完成RegEx模式。谢谢!
P.S。英语,我会说吗?..不,我很抱歉,但我希望你能理解我的想法
答案 0 :(得分:2)
试试这个RegEx:
\1<a href="mysite.com">awesome</a>
并替换为:
SharedPreferences sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
Editor editor = sharedpreferences.edit();
editor.putString("key_uri", "uri_value");
editor.commit();