0

I am trying to split the xpath as individual node.

My xpath is /samp/lorem/ipsum/dolar[a/c]/imet(a)/data

and I want to split like below:

/samp
/lorem
/ipsum
/dolar[a/c]
/imet(a)
/data

how to do this using java regex?

String nodes = xpath.split("?");

1
  • "how to do this using java regex?" You don't. XPath is a nested syntax. Java regex cannot do nested patterns. Commented Mar 5, 2019 at 17:32

1 Answer 1

1

You can split your string using this regex,

(?=/)(?![^\[\]]*])

Here (?=/) regex marks the position at the beginning of every / and (?![^\[\]]*]) negative look ahead ensures / inside square brackets is not selected for split.

Check this Java code,

String s = "/samp/lorem/ipsum/dolar[a/c]/imet(a)/data";
Arrays.stream(s.split("(?=/)(?![^\\[\\]]*])")).forEach(System.out::println);

Prints,

/samp
/lorem
/ipsum
/dolar[a/c]
/imet(a)
/data
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.