Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Regex to Capture 6-Digit Numbers

807589Sep 30 2008 — edited Oct 1 2008
I'm trying to create a regex Pattern that will capture all occurrences of 6-digit integer values in a string. I don't want any subscripts of larger strings of digits - only numbers exactly 6 digits in length.

I'm stumped. Everything I've tried misses something or captures something it shouldn't.
        String s = "123456the quick brown fox5551212 jumped 234567,345678, 456789.567890";
        Pattern pattern = Pattern.compile("(\\d{6})");
        Matcher matcher = pattern.matcher(s);

        while (matcher.find())
                System.err.println(matcher.group(1));
Should produce:

123456
234567
345678
456789
567890

I've tried bounding the capture group (\\d{6}) with combinations of \\D, \b, etc. to no avail.

Thanks.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 29 2008
Added on Sep 30 2008
5 comments
1,964 views