Results 1 to 2 of 2
  1. #1
    nmvictor is offline Member
    Join Date
    Oct 2009
    Posts
    31
    Rep Power
    0

    Unhappy Will someone pliz help me with this code on pattern matching

    Java Code:
     
    //In the code below, the matched string must be in the form XDD/DDDD/DDDD 
     //or XDDm/DDDD/DDDD   where 
     //X must be a upper-case-letter from A through Z
     //D can be any number from 0 through 9.
     //and m can be any lower-case-letter from a through z.
    //****************
     //the method getMatchedString should return the matchString if and only 
     //matchedString matches either XDD/DDDD/DDDD or XDDm/DDDD/DDDD
     //the delimiter "/" is inclusive of the pattern.
    
    
    public  String getMatchedString()
        {
            System.out.println("Please reply with a string: ");
            Pattern p = Pattern.compile("^[A-Z]\\d{2}/\\d{4}/\\d{4}$");
            Pattern k = Pattern.compile("^[A-Z]\\d{2}[a-z]{1}/\\d{4}/\\d{4}$");
             String matchedString = input.next();
             boolean match = true;
             boolean firstMatch = p.matcher(matchedString).find();
             boolean secondMatch = k.matcher(matchedString).find();
              
                   while(firstMatch != match || secondMatch != match ){
                    System.out.println("Invalid string!Try again.");
                  matchedString = input.next();
                   }
            
            return matchedString; 
       }
    //thanks in advance
    Last edited by nmvictor; 10-27-2009 at 11:23 AM.

  2. #2
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    6

    Default

    It seems your not familial with regex syntax, since you can easily combine the two patterns using a "?" to make the optional part optional (code below). Check out regular-expressions.info, which has an excellent regex tutorial.

    In your example, the second pattern has the lower-case letter as optional. To make something optional add a "?" afterwards. This is the same as adding the repetition quantifier {0,1}.

    Java Code:
    String regex = "^[A-Z]\\d{2}[COLOR="Red"][a-z]?[/COLOR]/\\d{4}/\\d{4}$"


    As for what you want to do with the regex, it's unclear to me. It seems that you want to check if the input matches the pattern. If it doesn't, you ask for a new input. However, you don't reset your matcher with the new input, nor do you try to perform a match with the new input, so if it's false the first time, you'll get an infinite loop.

    I think the below code is similar to what you want, but not sure (looking for feedback).

    Java Code:
    // used to simulate user input
    String[] inputs = {"!", "-", "a word", "W"};
    int index = 0;
    		
    String regex = "\\w";
    Pattern pattern = Pattern.compile(regex);
    
    String matchedString = inputs[index++];
    System.out.println("Trying: " + matchedString);
    Matcher matcher = pattern.matcher(matchedString);
    		
    while (!matcher.matches())
    {
    	matchedString = inputs[index++];
    	System.out.println("Input failed, trying: " + matchedString);
    			
    	//try with next input
    	matcher.reset(matchedString);
    }
    		
    System.out.println("Matched: " + matchedString);
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

Similar Threads

  1. Replies: 3
    Last Post: 03-19-2009, 04:01 AM
  2. Replies: 11
    Last Post: 04-16-2008, 01:52 PM

Posting Permissions

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