I would like to split a string using multiple delimiters.  Right now I am using this: 
String delims = "[\\s;.,:'!?()]";
 which seems to work fine for those characters, but when I try to add the - character, it yells at me.  How can I use all of these characters plus - as delimiters to split my string?  Thanks in advance! 
            Asked
            
        
        
            Active
            
        
            Viewed 8,702 times
        
    1
            
            
         
    
    
        mkjo0617
        
- 33
- 1
- 3
- 8
- 
                    Where are you putting that character? That would be read as a range in a regex and that is probably why it doesn't like it. – squiguy Apr 02 '13 at 21:15
- 
                    It yells. Cool. What compiler or runtime do you use that has this kind of error reporting? – Hauke Ingmar Schmidt Apr 02 '13 at 21:15
- 
                    I tried to just put it in amongst the others- where should I put it so that it knows that I want to use it single character as a delimiter? – mkjo0617 Apr 02 '13 at 21:17
- 
                    1Can you try and keep the dash at either the end or the beginning of your square brackets ... e.g.[\\s;.,:'!?()-] – jsshah Apr 02 '13 at 21:18
- 
                    I'm using Eclipse...it gives me a regex error- "illegal character range near index 5" if I try to add \- or |- to the existing string – mkjo0617 Apr 02 '13 at 21:19
- 
                    I used it as the last character and it worked. Thank you so much! I feel like an idiot... – mkjo0617 Apr 02 '13 at 21:21
2 Answers
5
            
            
        - inside the character class has a special meaning. It is usually used to select a range of characters like: [a-z] ... In order to match the dash alone ... either keep it in the beginning or the end
I just tried this and it worked
  String regex = "[\\s;.,:'!?()-]";
  String text = "jatin-shah-testing";
  String[] tokens = text.split(regex);
  for(int i = 0; i < tokens.length; i++)
    System.out.println(tokens[i]);
 
    
    
        jsshah
        
- 1,741
- 1
- 10
- 18
1
            - has a special meaning in character classes, indicating ranges. (E.g. [0-9] will match any digit.)  
However, if you put if you put it either as the first character or the last it will be matched as a literal -.
 
    
    
        Keppil
        
- 45,603
- 8
- 97
- 119
- 
                    Oh, that makes sense. I tried it as the last character and it worked perfectly. Thank you! – mkjo0617 Apr 02 '13 at 21:21
- 
                    1@mkjo0617: You're very welcome. Don't forget to upvote helpful answers, and select the answer that you feel helped you the most as the correct one. – Keppil Apr 02 '13 at 21:28