[英]Find and Replace String between two values
I want to find and replace values like: 我想查找和替换类似的值:
<TAG>heading<foo></foo></TAG><foo>juergen</foo>
goal: 目标:
<TAG>heading</TAG><foo>juergen</foo>
I want to remove the <foo>
Tags between <TAG></TAG>
我想删除<TAG></TAG>
之间的<foo>
标签
Here is my attempt: 这是我的尝试:
replaceAll("</?foo\\b[^>]*>", "");
Assuming that foo
is empty, you can use: 假设foo
为空,则可以使用:
<([^/][^>]*)></\1>
This searches for an opening tag with an adjacent closing tag of the same name. 这将搜索具有相同名称的相邻结束标签的开始标签。
You could augment it to allow for whitespace in the middle with: 您可以通过以下方式扩充它以允许在中间留有空格:
<([^/][^>]*)>\s*</\1>
Possible duplicate RegEx match open tags except XHTML self-contained tags 可能重复的RegEx匹配打开标签,但XHTML自包含标签除外
Otherwise, here is the regex, do not even ask me to explain, I barely know myself (this is in javascript, some corrections may need to be made for java): 否则,这里是正则表达式,甚至不要求我解释,我几乎不了解自己(这是在javascript中,可能需要对Java进行一些更正):
var txt = "<TAG>a<foo>b</foo>c</TAG>d<foo>e</foo>f<TAG>g<foo>h</foo>i</TAG>j<TAG>k</TAG>";
var res = txt.replace(/(<TAG>.*?)<foo>.*?<\/foo>(.*?<\/TAG>)/gm,"$1$2");
// ( $1 ) ( $2 )
String result = searchText.replaceAll("(<f.*><.*o>)(?=<)", "");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.