In the previous answer it used #?
as a delimiter. Here you write \*?
which means match *
literally or nothing. .*?
will then match anything until :
. That means multiple replacements will occur and strip anything until a colon (included). You should have multiple ;
in your replaced string.
The pattern I see is:
* (...) : ... comment 1<br/>
With <br/>
optional.
Thus try a regex like:
\* \([^)]*\) : ((?:.*?)comment \d+)(?:<br/>)?
Note that (?: ... )
makes the parenthesized regex non-grouping. You can check the result at http://rubular.com/r/orViHMJEMr
The regex provided here allows you to get the comments.
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TestRegex {
public static void main(String[] args) {
String pattern_string = "\\* \\([^)]*\\) : ((?:.*?)comment \\d+)(?:<br/>)?";
String input_string = "* (20-01-2012 12:53) : ---- comment 3<br/>* (20-01-2012 12:50) : comment 2<br/>* (20-01-2012 12:50) : comment 1";
Pattern pattern = Pattern.compile(pattern_string);
Matcher matcher = pattern.matcher(input_string);
while (matcher.find()) {
System.out.println(matcher.group(1));
}
}
}
* (...) : ... With
optional. and comment is not fixed, i just used comment1 ,2,3 as examples, so this is looking a little bit more complicated than i anticipated ... – lisadesouza89 Jan 20 '12 at 09:51