user3237736 user3237736 - 1 year ago 99
Java Question

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);

But this gives me incorrect results. I read the API doc about the
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.


Answer Source

Try the following expression:


The needed substring will be in the first group.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download