public String getContextName() {
String contextName = FacesContext.getCurrentInstance()
.getExternalContext().getRequestContextPath();
String uri = "localhost/gar/garmin-first/gar_home";
Pattern pat=Pattern.compile("/(.*?)/(.*)/");
Matcher matcher = pat.matcher(uri);
matcher.find();
System.out.println("matched"+matcher.group(1)+matcher.group(2));
if (StringUtil.isNotEmpty(contextName) &&
contextName.contains(matcher.group(1))) {
return matcher.group(2);
}
return matcher.group(1);
}
The output in the console will be printed as group(1) = gar,and group(2) = garmin-first, but what I really need is one regular expression that can work for both the cases. The other case is:
String uri = "localhost/garmin-first/gar_home";
In this case I need the output as group(1) = garmin-first and group(2) should be left empty. Can you please help me out
with a regular expression that can work for both the cases please.
I’ve changed the regex subtly so that we are looking for words that are surrounded by “/”.
Hopefully you can find a useful way to get the parts you need because I don’t think .group(1) and .group(2) will work now that we are looking for multiple matches in the same string.