0

Ok, you might say that this is a duplicate post but it is different.

I am working on a program that is working on some kind of deleting delimiters specified by the user. My program is working if the delimiter is only a single character (special or not). However, if the user input is a string, it removes the all characters of the delimiter from the message string.

ex. String message = "ab\nc[d]e{fMardk1g(h)i}j"; output will be : bcefghij but the expected output is abcdefghij

I'm new in using the Pattern class, so I don't know where the problem lies.

Here's the code in question (I put it in a testing class so I can isolate the problem):

import java.util.regex.Pattern;

public class ParsingTest {
    public static void main(String[] args) {
        String[] delimiters = { "Mardk1", "\n", "[", "]", "{", "}", "(", ")" };  
        StringBuilder regexp = new StringBuilder("");  
        regexp.append("[");  
        for(String s : delimiters) {  
            regexp.append("[");  
            regexp.append(Pattern.quote(s));  
            regexp.append("]");  
        }  
        regexp.append("]");  

        String message = "ab\nc[d]e{fMardk1g(h)i}j";  
        StringBuilder result = new StringBuilder("");  
        String[] a = message.split(regexp.toString());  
        for(String string : a) {  
            result.append(string);
        }
        System.out.println(result);
        for(String str: a) System.out.print(str);
        System.out.println();
    }
}
1
  • Your generated regex will contain [Mardk1] which will delimit by any one of the characters M,a,r,d,k or 1, and this is why you don't see a character in your output. Commented Jul 30, 2011 at 15:31

1 Answer 1

1

You're using the wrong kind of grouping construct. You're building a pattern like [xyz] which will match any single character x, y or z. You want to match any of several full strings, so you want the normal () style grouping, and the alternation operator (|). Have a look at the Pattern documentation for more details.

Try this instead to build up the regex:

for(String s : delimiters) {
    // We don't want to start with (|
    if (regexp.length() > 1)
    {
        regexp.append("|");
    }
    regexp.append(Pattern.quote(s));  
}  
Sign up to request clarification or add additional context in comments.

2 Comments

oh, tnx...you've solved my problem. Why it is necessary to have "|" in between the words
@JinShin: That's the alternation operator to say it matches this or that or (etc). So "(ab|cd|ef)" will match "ab", "cd" or "ef".

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.