0

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!

3
  • Use \\*(.*?)\\*, i.e. not greedy. Commented Sep 11, 2016 at 22:50
  • what do mean by "greedy"? thanks Commented Sep 11, 2016 at 22:56
  • "Greedy" means that if there is more than one way to get a match, it will choose the match with the largest number of characters. If your regex is a.*b, and your input string is a1111b...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 match a1111b. Commented Sep 12, 2016 at 1:39

1 Answer 1

1

Try the following expression:

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

The needed substring will be in the first group.

Sign up to request clarification or add additional context in comments.

3 Comments

You don't need to escape * inside a character class. \\*([^*]+)\\* or [*]([^*]+)[*] will do.
great, thanks you guys! but I don't quite understand: what exactly is the difference now to my approach? It seems it's the usage of the squared brackets, but I don't understand why it's neccessary?
Square brackets allow matching anything but not an asterisk, because an asterisk ends the phrase you want to match. For more information on regular expressions, see for example this article at WikiBooks: en.wikibooks.org/wiki/Regular_Expressions/… and StackOverflow Documentation: stackoverflow.com/documentation/regex/topics .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.