1

I have the same problem, but I need to get:

---- comment 3;comment 2;comment 1

From:

* (20-01-2012 12:53) : ---- comment 3<br/>* (20-01-2012 12:50) : comment 2<br/>* (20-01-2012 12:50) : comment 1

Which means my code should be:

string.replaceAll("\\*?.*?:", ";")

Correct?

But this is replacing everything from the first * to the last : so I'm getting:

comment 1 

as the result. Help!

Linked from Replace/remove String between two character

Community
  • 1
  • 1

3 Answers3

1

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));
        }
    }
}
Ludovic Kuty
  • 4,868
  • 3
  • 28
  • 42
  • this indeed works but damn! this to me is an overkill using groups, then optional groups - it took me a while to understand what you were doing :) nevertheless +1 from me – Eugene Jan 20 '12 at 08:19
  • The regex might seem complicated but I don't think it is. The group allows us to get what we need. The non-grouping is not necessary but I used it like some kind of optimization. The rest of the pattern has the same structure as the input we want to match. The `[^)]* is also some kind of optimization. I could have done `.*?` instead. – Ludovic Kuty Jan 20 '12 at 08:42
  • umm, ok, @lkuty, but the pattern is
    * (...) : ... 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
  • Then the problem was not clearly specified. Anyway the regex is adaptable. – Ludovic Kuty Jan 20 '12 at 11:15
1

myString = myString.replaceAll("\\*\\s+\\(.*?\\)\\s+:\\s+", "").replaceAll("<br/>", ";");

It works I've tried.First replaceAll replaces the * (date/time) : prefix from lines and second replaceAll replaces <br/>s with ;s.

shift66
  • 11,760
  • 13
  • 50
  • 83
  • ok looks like ALMOST the result i'm looking for, but i changed it to: `code`remark = remark.replaceAll("(\\*\\s+\\(.*?\\)\\s+)(.*?):\\s+", ""); remark = remark.replaceAll("
    ", ";");`code`
    – lisadesouza89 Jan 20 '12 at 10:04
0

Seems like you need to replace <br/>* (20-01-2012 12:50) : with ;.

A good matching regex could be:

(<br/>)\*\s*\(.*?\)\s*:\s*

The matching portion should be replaced with ;.

For the beginning part, the regex should be:

^\*\s*\(.*?\)\s*:\s*

which should be replaced with an empty string.

Sufian Latif
  • 13,086
  • 3
  • 33
  • 70
  • almost, but was adding an extra ; before the first comment as well... hmm, think @Ademiban is the closest to what i actually wanted to happen... thanks for the effort tho! :) – lisadesouza89 Jan 20 '12 at 10:07