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>
我想删除
Here is my attempt:
这是我的尝试:
replaceAll("</?foo\\b[^>]*>", "");
3 个解决方案
#1
1
String result = searchText.replaceAll("(<f.*><.*o>)(?=<)", "");
#2
1
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>
#3
1
Possible duplicate RegEx match open tags except XHTML self-contained tags
除XHTML自包含标记外,可能重复的RegEx匹配开放标记
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 )
#1
1
String result = searchText.replaceAll("(<f.*><.*o>)(?=<)", "");
#2
1
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>
#3
1
Possible duplicate RegEx match open tags except XHTML self-contained tags
除XHTML自包含标记外,可能重复的RegEx匹配开放标记
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 )