user840718
user840718

Reputation: 1611

Substring in Java with recurrent substring

I have this string:

file:/C:/workWaveMaker/projects/AAA/webapproot/WEB-INF/classes/custom/

My goal is to parse only the string AAA in this case, but I will face othere similar strings where AAA is not the string, but something different. Is there a way to solve this based for example on the recurrent string webapproot?

Upvotes: 0

Views: 73

Answers (1)

Rod_Algonquin
Rod_Algonquin

Reputation: 26198

Use the Pattern class using regex to extract the AAA

String s = "file:/C:/workWaveMaker/projects/AAA/webapproot/WEB-INF/classes/custom/";
Pattern p = Pattern.compile("/projects/(.*?)/webapproot/");
Matcher m = p.matcher(s);
if (m.find()) 
  System.out.println(m.group(1)); // => result "AAA"

Upvotes: 3

Related Questions