Java tutorial
//package com.java2s; //License from project: Apache License public class Main { public static String insideTopLevel(String xpath) { boolean alreadyInsideTopLevel = xpath.matches("^[(]*[//]+.*"); final String prefix = (alreadyInsideTopLevel) ? "" : (xpath.startsWith("(")) ? "(//" : "//"; final int chopn = (xpath.startsWith("(") && !alreadyInsideTopLevel) ? 1 : 0; return prefix + xpath.substring(chopn); } }