[英]Using replaceAll to remove part of XPath expression
String xpath = "A/B/C[Hello world]/D";
In the above string I have to replace the square brackets and the content between it. 在上面的字符串中,我必须替换方括号和它之间的内容。 Final output should be:
A/B/C/D
. 最终输出应为:
A/B/C/D
I have written the below code but it does not work if there is aa space between Hello
and World
: 我写了下面的代码,但如果
Hello
和World
之间有一个空格,它就不起作用:
String Xpath1 = xpath.replaceAll("\\[[\\S]+\\]", "");
Your regex matches all non-whitespace characters. 你的正则表达式匹配所有非空白字符。 Therefore, it will not match a space.
因此,它不会匹配空间。
It sounds like you actually want to match all characters except ]
: 听起来你真的想要匹配除了
]
之外的所有字符:
[^\\]]
这应该做的伎俩:
String xpathResult = xpath.replaceAll("\\[.*\\]", "");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.