Results 1 to 3 of 3

Thread: Help with regex

  1. #1
    Join Date
    Mar 2009
    Posts
    31
    Rep Power
    0

    Default Help with regex

    I have a program in which i'm creating TelephoneNumber objects which have ints areaCode exchangeCode and number. I'm using a regex to make sure that the input is an actual phone number. I want to know if there is a way to use a back reference to assign the first three numbers to areaCode, the second 3 to exchange code and the last 4 to number. My code is below

    Java Code:
    private static int areaCode, exchangeCode, number;
     public TelephoneNumber(String phoneNumber){
        Pattern pattern = Pattern.compile("(\\d{3})-(\\d{3})-(\\d{4})");
        Matcher matcher = pattern.matcher(phoneNumber);
        if(matcher.matches())
            System.out.println("valid phone number");
        else 
            System.out.println("invalid phone number");
        }
    Back reference 1 needs to equal to areaCode, 2 equal to exchangeCode and 3 to number but I don't know how to do this.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Please don't post the same question multiple times. Thank you for your cooperation.

  3. #3
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    5

Similar Threads

  1. Using regex
    By SteroidalPsycho in forum New To Java
    Replies: 0
    Last Post: 03-28-2010, 11:07 PM
  2. Regex approach
    By karlito in forum Lucene
    Replies: 1
    Last Post: 11-04-2009, 05:53 PM
  3. Interpretation of regex?
    By Ms.Ranjan in forum New To Java
    Replies: 2
    Last Post: 04-23-2009, 06:37 PM
  4. [SOLVED] More RegEx help
    By JT4NK3D in forum New To Java
    Replies: 2
    Last Post: 05-23-2008, 04:07 AM
  5. Regex Quantifiers Example
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-10-2008, 10:44 AM

Posting Permissions

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