I want to split my String (e.g. "20150101") using Regular Expression.
 For example I need these values: "2015","01","01"
            Asked
            
        
        
            Active
            
        
            Viewed 113 times
        
    -3
            
            
         
    
    
        Sahar Rabet
        
- 15
- 3
- 
                    6Use `String.split(String regex)`? There are tons of questions on SO about using regex. What have you tried? – mkobit Apr 29 '15 at 21:46
- 
                    1Aren't you actually trying to parse this as a date instead? See for example http://stackoverflow.com/questions/28034202/simple-way-to-convert-yyyymmdd-formatted-string-into-a-gregoriancalendar-objec – Didier L Apr 29 '15 at 22:02
- 
                    possible duplicate of [How to split a string in Java](http://stackoverflow.com/questions/3481828/how-to-split-a-string-in-java) – Skatox Apr 29 '15 at 22:06
4 Answers
3
            String pattern = "(....)(..)(..)";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(inputString);//inputString:"20150101"
Now you can use m.group(x) to get the parts of the string. For example:
m.group(1) is first four digit ("2015" in your question).
 
    
    
        Nate Barbettini
        
- 51,256
- 26
- 134
- 147
 
    
    
        Mohammadreza Tavakoli
        
- 420
- 4
- 5
3
            
            
        Bit hard to say without more details, but try:
(\d{4})(\d{2})(\d{2})
Your Matcher's three captured group references will then have the values you want.
 
    
    
        declension
        
- 4,110
- 22
- 25
0
            
            
        Just combining the two answers both are valid
First
public static void main(String[] args) {
            String input = "20150101";
            String pattern = "(....)(..)(..)";
            Pattern r = Pattern.compile(pattern);
            Matcher m = r.matcher(input);       
            m.find();
            for(int i=1;i<=m.groupCount();i++){
                String token = m.group( i ); 
                  System.out.println(token);
            }
    }
Second
public static void main(String[] args) {
            String input = "20150101";
            String pattern = "(\\d{4})(\\d{2})(\\d{2})";
            Pattern r = Pattern.compile(pattern);
            Matcher m = r.matcher(input);       
            m.find();
            for(int i=1;i<=m.groupCount();i++){
                String token = m.group( i ); 
                  System.out.println(token);
            }
    }
 
    
    
        Shahzeb
        
- 4,745
- 4
- 27
- 40
0
            
            
        private static final Pattern DATE_PATTERN = Pattern.compile("^([\\d]{4})([\\d]{2})([\\d]{2})$");
public static Optional<String[]> split(String str) {
    final Matcher matcher = DATE_PATTERN.matcher(str);
    if (matcher.find()) {
        final String[] array = new String[3];
        array[0] = matcher.group(1);
        array[1] = matcher.group(2);
        array[2] = matcher.group(3);
        return Optional.of(array);
    }
    return Optional.empty();
}
 
    
    
        Karol Król
        
- 3,320
- 1
- 34
- 37