Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
String xpath = "A/B/C[Hello world]/D";
在上面的字符串中,我必须替换方括号及其之间的内容。最终输出应该是:A/B/C/D.
A/B/C/D
Hello我已经编写了以下代码,但如果和之间有空格,则它不起作用World:
Hello
World
String Xpath1 = xpath.replaceAll("\\[[\\S]+\\]", "");
您的正则表达式匹配所有非空白字符。因此,它不会匹配空格。
听起来您实际上想要匹配所有字符,除了]:
]
[^\\]]
这应该可以解决问题:
String xpathResult = xpath.replaceAll("\\[.*\\]", "");