0

I have a string that recieved while parsing XML-document:

"ListOfItems/Item[Name='Model/Id']/Price"

And I need to split it by delimeter - "/" String[] nodes = path.split("/") , but with one condition:

"If backslash presence in name of item, like in an example above, I must skip this block and don't split it."

ie after spliting a must get next array of nodes:

ListOfItems, Item[Name='Model/Id'], Price

How can I do it using regex expression?

Thanks for help!

2 Answers2

3

You can split using this regex:

/(?=(?:(?:[^']*'){2})*[^']*$)

RegEx Demo

This regex basically splits on only forward slashes / that are followed be even number of single quotes, which in other words mean that / inside single quotes are not matched for splitting.

anubhava
  • 761,203
  • 64
  • 569
  • 643
0

A way consists to use this pattern with the find method and to check if the last match is empty. The advantage is that you don't need to add an additional lookahead to test the string until the end for each possible positions. The items you need are in the capture group 1:

\\G/?((?>[^/']+|'[^']*')*)|$

The \G is an anchor that matches either the start of the string or the position after the previous match. Using this forces all the matchs to be contiguous.

(?>[^/']+|'[^']*')* defines the possible content of an item: all that is not a / or a ', or a string between quotes.

Note that the description of a string between quotes can be improved to deal with escaped quotes: '(?>[^'\\]+|\\.)*' (with the s modifier)

The alternation with the $ is only here to ensure that you have parsed all the string until the end. The capture group 1 of the last match must be empty. If it is null, this means that the global research has stopped before the end (for example in case of unbalanced quotes)

example

Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125