Results 1 to 3 of 3

Thread: Need help with regex{Resolved}

  1. #1

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Need help with regex{Resolved}

    Ive been trying to create a pattern that just matches three digits and three characters. Ive figured it out but in some instances it doesn't work quite right. Here's the pattern "(\\d{3}\\w{3})". "164Bdr" match, "121324" match but don't want to, "brd123" no match, "Brstd" no match.

    It seems the pattern is failing on the \\d. The quantifer is greedy so that might be the problem.

  2. #2
    Fanatic Member
    Join Date
    Jan 2005
    Location
    Cebu
    Posts
    607

    Re: Need help with regex

    Sounds fine by me.
    VB Code:
    1. import java.util.regex.*;
    2. public class test{
    3.     public static void main(String[] args){
    4.         Pattern p=Pattern.compile("\\d{3}\\w{3}");
    5.         System.out.println(p.matcher("164Bdr").matches());
    6.         System.out.println(p.matcher("121324").matches());
    7.         System.out.println(p.matcher("brd123").matches());
    8.         System.out.println(p.matcher("Brstd").matches());
    9.     }
    10. }
    Outputs
    VB Code:
    1. true
    2. true
    3. false
    4. false

  3. #3

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Re: Need help with regex

    Ah ok i think i get it. Using "(\\d{3}\\w{3})" on "121324" the \\d is for digit [0-9] and \\w is for word character [a-zA-Z0-9] so true was correct. I would have to use \\D non digit [^0-9] "(\\d{3}\\D{3})"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width