Java regex find last digit consecutively from 0-9 and first 6 digits are same -


i regular expression in java finds - first 6 digits same , - last digit range 0-9 consecutively.

1303250 1303251

1304150 1304151 1304152 1304153 1304154 1304155 1304156 1304157 1304158 1304159

in case expression match 130415x.

i developed 2 separate regex as

        pattern f6 = pattern.compile("^......");         pattern last = pattern.compile("\\d$"); 

so, write regex group first 6 digits (followed 0), , check group followed count number 9. find matches in string, , print out first grouping if match found.

here's code:

import java.util.regex.pattern; import java.util.regex.matcher;  public class helloworld{   public static void main(string []args){     string test = "1304150 1304151 1304152 1304153 1304154 1304155 1304156 1304157 1304158 1304159\r\n" +                   "5304150 5304151 5304152 5304153 5304154 5304155 5304156 5304157 5304158 5304159\r\n" +                    "7304150 7304153 71304156";      pattern p = pattern.compile("(\\d{6})0 (?:\\1)1 (?:\\1)2 (?:\\1)3 (?:\\1)4 (?:\\1)5 (?:\\1)6 (?:\\1)7 (?:\\1)8 (?:\\1)9", pattern.multiline);     matcher m = p.matcher(test);      while (m.find()) {         system.out.println(new string(m.group(1)) + "x");     }  } 

}

output:

130415x 530415x 

if finds match, prints out relevant 6 digits plus "x". see in action here.

regex explanation. essentially, groups first match of 6 digits followed 0. then, looks group followed 1, group followed 2, etc. double '\' throughout string escape character in java string, , regex should read without double slashes:

(\d{6})0 (?:\1)1 (?:\1)2 (?:\1)3 (?:\1)4 (?:\1)5 (?:\1)6 (?:\1)7 (?:\1)8 (?:\1)9

   node                     explanation --------------------------------------------------------------------------------   (                        group , capture \1:     \d{6}                    digits (0-9) (6 times)   )                        end of \1   0                        '0 '   (?:                      group, not capture:     \1                       matched capture \1   )                        end of grouping   1                        '1 '   (?:                      group, not capture:     \1                       matched capture \1   )                        end of grouping   2                        '2 '   (?:                      group, not capture:     \1                       matched capture \1   )                        end of grouping   3                        '3 '   (?:                      group, not capture:     \1                       matched capture \1   )                        end of grouping   4                        '4 '   (?:                      group, not capture:     \1                       matched capture \1   )                        end of grouping   5                        '5 '   (?:                      group, not capture:     \1                       matched capture \1   )                        end of grouping   6                        '6 '   (?:                      group, not capture:     \1                       matched capture \1   )                        end of grouping   7                        '7 '   (?:                      group, not capture:     \1                       matched capture \1   )                        end of grouping   8                        '8 '   (?:                      group, not capture:     \1                       matched capture \1   )                        end of grouping   9                        '9' 

as requested in comments, opposite can use:

^(?!((\d{6})0 (?:\2)1 (?:\2)2 (?:\2)3 (?:\2)4 (?:\2)5 (?:\2)6 (?:\2)7 (?:\2)8 (?:\2)9)).*$

as expected, inverse matches bit more complicated, here's explanation , can see in action.


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -