1

Would it be possible to change this:

[quote]user1 posted:
      [quote]user2 posted:
              Test1
      [/quote]
      Test2
[/quote]
Test3

to this:

Test3

Using Java 6?

  • 2
    Nested brackets cannot be matched using regex in Java. Better your use a custom parser code – anubhava Nov 04 '20 at 09:48
  • Replace `[` by ``, parse as XML, do DOM traversal. – Sebastian Simon Nov 04 '20 at 09:48
  • 1
    It looks like you are looking to create a regex, but do not know where to get started. Please check [Reference - What does this regex mean](https://stackoverflow.com/questions/22937618) resource, it has plenty of hints. Also, refer to [Learning Regular Expressions](https://stackoverflow.com/questions/4736) post for some basic regex info. Once you get some expression ready and still have issues with the solution, please edit the question with the latest details and we'll be glad to help you fix the problem. Heads-up: yes, it is possible. – Wiktor Stribiżew Nov 04 '20 at 09:48
  • @WiktorStribiżew It’s weird to see this canned comment posted on something for which a reliable regex cannot be written… – Sebastian Simon Nov 04 '20 at 09:50
  • @user4642212 It can be written, I posted a similar solution for JavaScript, and Python. – Wiktor Stribiżew Nov 04 '20 at 09:51
  • 1
    Any particular reason why you’re using Java 6? It’s ancient, and no longer supported. Even the next version (Java 7) is nearing the end of its support. – Konrad Rudolph Nov 04 '20 at 09:57
  • 1
    @KonradRudolph hmmm... I working on some old system of spring that using java 6. And it imposable to update it now..... – Timur Misharin Nov 04 '20 at 10:02
  • @TimurMisharin You should definitely invest the time to update. Having an unsupported Java system hanging around is a both a maintenance *and* security nightmare. – Konrad Rudolph Nov 04 '20 at 10:04
  • 1
    @KonradRudolph is not the issue for now. The issue is to remove a text. – Timur Misharin Nov 04 '20 at 10:06

2 Answers2

2

ok, wrote some not regex solution.

    String str ="[quote]user1 posted:[quote]user2 posted:Test1[/quote]Test2[/quote]Test3";
    String startTag = "[quote]";
    String endTag = "[/quote]";
    String subStr;
    int startTagIndex;
    int endTagIndex;
    while(str.contains(startTag) || str.contains(endTag)) {
        startTagIndex = str.indexOf(startTag);
        endTagIndex = str.indexOf(endTag) + endTag.length();
        if(!str.contains(startTag)) {
            startTagIndex = 0;
        }
        if(!str.contains(endTag)) {
            endTagIndex = startTagIndex + startTag.length();
        }
        subStr = str.substring(startTagIndex, endTagIndex);;
        str = str.replace(subStr, "");
    }
1

I compiled this to Java 8. I don't believe I'm using any features not available in Java 6.

Edited: System.lineSeparator() was added in Java 1.7. I changed the line to System.getProperty("line.separator").

public class RemoveQuotes {

    public static void main(String[] args) {
        String input = "[quote]user1 posted:\r\n" + 
                "      [quote]user2 posted:\r\n" + 
                "              Test1\r\n" + 
                "      [/quote]\r\n" + 
                "      Test2\r\n" + 
                "[/quote]\r\n" + 
                "Test3";
        
        input = input.replace(System.getProperty("line.separator"), "");
        String endQuote = "[/quote]";
        int endPosition;
        
        do {
            int startPosition = input.indexOf("[quote]");
            endPosition = input.lastIndexOf(endQuote);
            if (endPosition >= 0) {
                String output = input.substring(0, startPosition);
                output += input.substring(endPosition + endQuote.length());
                input = output;
            }
        } while (endPosition >= 0);
        
        System.out.println(input);
    }

}
Gilbert Le Blanc
  • 45,374
  • 5
  • 61
  • 107
  • 1
    This removes the entire text from the first occurrence of `"[quote]"` to the last occurrence of `"[/quote]"`, hence, it is impossible to have another matching range in the result, so it is unnecessary to put this in a loop. Since this doesn’t care for actual nesting, it’s possible to formulate an equivalent regex, i.e. `input = input.replaceFirst("\\[quote].*\\[/quote]", "");` does the same. – Holger Nov 12 '20 at 17:30