Results 1 to 10 of 10
  1. #1
    dcarter's Avatar
    dcarter is offline Student
    Join Date
    Sep 2014
    Location
    NC
    Posts
    14
    Rep Power
    0

    Default Problem matching a regex

    I am attempting to use a regular expression for data validation in a client/server program where in the client side the user enters a temperature in the form of some floating point or integer number ending with either "F" for "C". Examples of valid data: 12C, 39F, 3.78C, 45.90F, 105F, 0.3C, etc. Basically there has to be one or more digit before an optional decimal followed by 0 or more digits and ending with either F or C. I realize that this can allow something like 12.C, but I am not too concerned with that right now. After valid input is found, I want to save the F or C character in the variable fc and then create a substring of just the number part, then pass those two things to the server where the calculation is performed and then the appropriate F or C is appended to the result, then passed back to the client and displayed.

    I created a regex and have tested it numerous times on sites like regexr.com and I find no errors with my regular expression; however, when I run my code it doesn't recognize ANY valid data. It says everything is invalid. I am guessing this means I have some sort of error in my logic, but I can't find it. I am hoping you guys can help me. I am posting only the section that asks for user input, but I can post more if needed.

    Java Code:
    String clientTemp;
    char fc;
    double ct;
    String servTemp;
    String reg = "/[0-9]+.?[0-9]*(F|C)/";
    boolean validEntry = false;
    
    while (validEntry == false) {
        System.out.print("Enter a temperature to convert: ");
        clientTemp = input.nextLine();
    
        if (clientTemp.matches(reg)){
    		validEntry = true;
    		fc = clientTemp.charAt((clientTemp.length() - 1));
    		clientTemp = clientTemp.substring(0, (clientTemp.length() - 1));
    		ct = Double.parseDouble(clientTemp);
    						
    		PrintStream output = new PrintStream(sock.getOutputStream());
    	        output.println(ct);
    	        output.println(fc);
    	} else {
    		System.out.println("Invalid entry.");
    	}				
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,994
    Rep Power
    32

    Default Re: Problem matching a regex

    Can you post a short, simple program that tests your regex to show that it works?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: Problem matching a regex

    A dot is a special character in regular expressions; you have to escape it \. Also, what are those slashes doing at the start and end of your expression? btw (F|C) is written as [FC] in regular expression lingo.

    kind regards,

    Jos
    Last edited by JosAH; 03-12-2016 at 08:28 AM.
    Build a wall around Donald Trump; I'll pay for it.

  4. #4
    dcarter's Avatar
    dcarter is offline Student
    Join Date
    Sep 2014
    Location
    NC
    Posts
    14
    Rep Power
    0

    Default Re: Problem matching a regex

    Quote Originally Posted by JosAH View Post
    A dot is a special character in regular expressions; you have to escape it \. Also, what are those slashes doing at the start and end of your expression? btw (F|C) is written as [FC] in regular expression lingo.

    kind regards,

    Jos
    I learned regular expressions when using Ruby, that may be my problem. Are there any java-specific sites for testing that a regular expression works? Because it has worked as-is on every site I've tried it on.

    EDIT: If I escape the decimal, Eclipse tells me I have an invalid escape character in my regex and won't let me compile the program. Changing (F|C) to [FC] did not make it work either.

    EDIT2: If I remove the forward slashes at the beginning and end, when I go to test the program and input a temperature then the program just gets stuck. I hit enter and it goes to the next line and doesn't progress. It no longer says "Invalid entry." but it doesn't go through the 'if' section either. It does nothing.
    Last edited by dcarter; 03-12-2016 at 12:44 PM.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: Problem matching a regex

    The entire regexp specification can be found in the API docs for the Pattern class; if you want to pass a \. to the regexp compiler, you are first passing it through javac, the Java compiler and, indeed \. isn't a valid escape sequence for Java; the remedy is simple: escape that escape character itself: \\.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,994
    Rep Power
    32

    Default Re: Problem matching a regex

    testing that a regular expression works?
    Write a one line java program that prints out the value returned by matches:
    Java Code:
     System.out.println("3.4F".matches("/[0-9]+.?[0-9]*(F|C)/"));
    Last edited by Norm; 03-12-2016 at 01:25 PM.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Problem matching a regex

    To escape a . you need to use \\. The \\ escapes a slash for the String so it will not process \ as its own escape. Then the \. will be passed to
    the regex engine to escape the . special character.

    ETA:

    Just noticed this (Duh) - basically what Jos said 6 hours earlier.

    Regards,
    Jim
    Last edited by jim829; 03-12-2016 at 06:52 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: Problem matching a regex

    Quote Originally Posted by jim829 View Post
    Just noticed this (Duh) - basically what Jos said 6 hours earlier.
    Yep, I know; I'm so fast; if I weren't used to it I'd surprise myself; voooom, there I go again, but I don't do drugs except for an occasional Grolsch (famous Dutch beer!) or a dose of Van Nelle Zwaar (famous Dutch roll your own tobacco!) or loads of espresso coffee or a shot of Ketel 1 (famous Dutch genever!) ;-)

    kind regard,

    Speedy Gonzalez
    Build a wall around Donald Trump; I'll pay for it.

  9. #9
    dcarter's Avatar
    dcarter is offline Student
    Join Date
    Sep 2014
    Location
    NC
    Posts
    14
    Rep Power
    0

    Default Re: Problem matching a regex

    Quote Originally Posted by Norm View Post
    Write a one line java program that prints out the value returned by matches:
    Java Code:
     System.out.println("3.4F".matches("/[0-9]+.?[0-9]*(F|C)/"));
    Duh, that's so simple! Thanks. I fixed my regex (now it's "[0-9]+\\.?[0-9]*[FC]{1}") and it appears to be working!

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: Problem matching a regex

    What's that {1} part doing? It only states that the last regexp should occur exactly one time; [FC] does the same ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. Replies: 2
    Last Post: 01-13-2012, 02:13 PM
  2. Problem with Memory Matching Game
    By thesonyuh in forum New To Java
    Replies: 6
    Last Post: 09-24-2011, 04:59 AM
  3. matching problem
    By drgnfire25 in forum New To Java
    Replies: 4
    Last Post: 02-12-2011, 09:20 PM
  4. Regex - matching literal characters
    By racha0601 in forum Advanced Java
    Replies: 3
    Last Post: 04-07-2009, 11:25 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
  •