Today at work, I needed to come up with a regular expression that, given an xpath expression for a node, would return the xpath for that node’s parent. Originally, I wasn’t using regular expressions at all, but was just getting the substring of the xpath up to the index of the last occurrence of /. This works fine, so long as the xpath expression doesn’t contain any conditionals that contain a /, but what if you want to get the parent xpath from something like this:


If the substring approach is used, /a/b[@x='/1/2/3']/c[@y='/4/5 is returned instead of /a/b[@x='/1/2/3']! Hence the need for a regular expression.

I did some hacking around (as it turns out I was approaching things from the wrong point of view at first), did a few fruitless google searches, and finally gave up searching on

Finally, I figured it out all on my own after an “aha!” moment of clarity.


Behold! What this is doing is a greedy match of any character up to but not including a / that follows a [ without a ] in between. I hope this will help somebody down the road who has a similar problem to solve.