Rahul Jain
Rahul Jain

Reputation: 115

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.

I have written the below code but it does not work if there is a a space between Hello and World:

String Xpath1 = xpath.replaceAll("\\[[\\S]+\\]", "");

Upvotes: 0

Views: 449

Answers (2)

Indu Devanath
Indu Devanath

Reputation: 2188

This should do the trick:

String xpathResult = xpath.replaceAll("\\[.*\\]", "");

Upvotes: 0

SLaks
SLaks

Reputation: 887479

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 ]:

[^\\]]

Upvotes: 2

Related Questions