Results 1 to 6 of 6

Thread: regex question

  1. #1
    jruland is offline Member
    Join Date
    Mar 2010
    Posts
    5
    Rep Power
    0

    Default regex question

    i am trying to create a regular expression that says the following:

    allow any number for the first 6 digits. after that, do not allow any character besides a '.' and after that, allow 2 digits of any number. so, in other words, i want to allow a maximum of 6 digits followed by a maximum of 2 decimal places. my current regular expression doesn't work, but it goes as follows:

    "[0-9]{0," + MAX_INTEGER_PRECISION + "}[\\.]{0,1}[0-9]{0," + MAX_DECIMAL_PRECISION + "}"

    any idea where i went wrong here?

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default

    any idea where i went wrong here?
    You didn't tell us where MAX_INTEGER_PRECISION and MAX_DECIMAL_PRECISION came from.

    You didn't post sample inputs along with mentioning whether they should be matched or not

    You didn't post a SSCCE
    SSCCE : Java Glossary

    You said "doesn't work" which is meaningless without clarifying exactly what result you got.

    db

  3. #3
    ceautery is offline Member
    Join Date
    Sep 2010
    Location
    Columbus, Ohio
    Posts
    4
    Rep Power
    0

    Default

    Your example regex I believe does what it's supposed to. Maybe your problem is somewhere else. Here is what I got when testing:

    Java Code:
    import java.util.regex.Pattern;
    public class Test {
      public static void main(String[] args) {
        int MAX_INTEGER_PRECISION = 6, MAX_DECIMAL_PRECISION = 2;
        String justRight = "1234.1", tooLong = "1234567.123", noDecimal = "12345", noNums = "abc.ef";
        String regex = "[0-9]{0," + MAX_INTEGER_PRECISION + "}[\\.]{0,1}[0-9]{0," + MAX_DECIMAL_PRECISION + "}";
        Pattern p = Pattern.compile(regex);
        System.out.println("Just right matches? : " + p.matcher(justRight).matches());
        System.out.println("Too long matches? : " + p.matcher(tooLong).matches());
        System.out.println("No decimal matches? : " + p.matcher(noDecimal).matches());
        System.out.println("No numbers matches? : " + p.matcher(noNums).matches());
      }
    }
    Output:
    Just right matches? : true
    Too long matches? : false
    No decimal matches? : true
    No numbers matches? : false

  4. #4
    jruland is offline Member
    Join Date
    Mar 2010
    Posts
    5
    Rep Power
    0

    Default

    thanks for taking the time to check this out. hmmm it certainly is possible the error is elsewhere. my coworker and i spent a lot of time just staring at that string and trying to figure out where we went wrong, but obviously we couldn't. i'll check out other areas of the code and see what i can see.

  5. #5
    audriusa is offline Member
    Join Date
    Oct 2010
    Posts
    7
    Rep Power
    0

    Default

    I have tested

    [0-9]{0,3}[\.]{0,1}[0-9]{0,4}

    here, it really works exactly as required in the initial posting (assuming the given values for the constants). Your regular expression is correct.

  6. #6
    jruland is offline Member
    Join Date
    Mar 2010
    Posts
    5
    Rep Power
    0

    Default

    i see. in that case you are right, it must be a problem somewhere else in my code.

Similar Threads

  1. Using Regex
    By ron87 in forum New To Java
    Replies: 4
    Last Post: 09-12-2010, 06:26 AM
  2. Please Help - Regex
    By BeeGee in forum Advanced Java
    Replies: 0
    Last Post: 04-28-2010, 05:28 PM
  3. Help with regex
    By SteroidalPsycho in forum New To Java
    Replies: 2
    Last Post: 03-29-2010, 12:40 AM
  4. Using regex
    By SteroidalPsycho in forum New To Java
    Replies: 0
    Last Post: 03-28-2010, 11:07 PM
  5. regex question
    By mac in forum New To Java
    Replies: 6
    Last Post: 03-17-2010, 05:57 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
  •