I want to extract all sub-strings from a string that are enclosed in certain tags. For example, if I have an input string that encloses some sub-strings in "*" tags:
I contain two terms to extract: *first term* and *second term*
What I want to get is the two sub-strings "first term" and "second term". I tried with the following code:
List<String> matches = new ArrayList<>();
Matcher m = Pattern.compile(".*\\*(.*)\\*.*").matcher(inputString);
while(m.find()){
matches.add(m.group(1));
}
But this gives me incorrect results. I read the API doc about the group method, but to be honest I don't quite understand what it means and how it works. I'd be thankful if someone could tell me what the best approach is to gather all wanted sub-strings here.
Thanks!
\\*(.*?)\\*, i.e. not greedy.a.*b, and your input string isa1111b...a2222b, this will match the entire string, since it chooses the largest possible match for.*. By adding a question mark,a.*?b, you tell it to choose the match with the smallest number of characters. So this would matcha1111b.