user3237736
user3237736

Reputation: 867

Java RegEx: Extract multiple sub-strings from a string

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!

Upvotes: 0

Views: 2590

Answers (1)

Andrew Sklyarevsky
Andrew Sklyarevsky

Reputation: 2135

Try the following expression:

\\*([^\\*]+)\\*

The needed substring will be in the first group.

Upvotes: 1

Related Questions