Page 1 of 2 12 LastLast
Results 1 to 20 of 23
Like Tree1Likes

Thread: Simple Regular Expression - first time in Java

  1. #1
    mallorz is offline Member
    Join Date
    Jan 2014
    Posts
    68
    Rep Power
    0

    Default Simple Regular Expression - first time in Java

    Hello,

    I am currently enrolled in my first Java class. I have taken C# in the past so I've messed with some regular expressions, but I am having trouble finding a good website for Java. Is the syntax the same?

    I want to create a regular expression to only allow the following characters: c C f F [and any 1-3 digit number]. Could someone assist?

    Thank you.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,595
    Rep Power
    5

    Default Re: Simple Regular Expression - first time in Java

    Here is the online site for the JDK API. Java Platform SE 7. You should check out the Pattern class.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,972
    Rep Power
    5

    Default Re: Simple Regular Expression - first time in Java

    The syntax (of the regular expressions, not the API to invoke them) is mostly the same. So far any regular expression information I have found on the net, which is usually not related to Java, has worked. But my first stop is always the javadoc of the Pattern class. In case you are not aware, its very easy to find specific javadocs. In this case just type

    java 7 Pattern

    into google and the first hit should be it. Pattern (Java Platform SE 7 )
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    mallorz is offline Member
    Join Date
    Jan 2014
    Posts
    68
    Rep Power
    0

    Default Re: Simple Regular Expression - first time in Java

    \d{1,3} = Any digit, at least one not more than 3
    \p = A white-space character (this should be optional)
    [cf-CF] = lowercase or uppercase C or F only

    COMPLETED: \d{1,3}\p[cf-CF]

    The above is what I came up with. I ran it through a tester (link below) and at first all results failed (no matches) then I started to get an error for an invalid character.

    RegexPlanet: online regular expression testing for Java

    Am I close?

  5. #5
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: Simple Regular Expression - first time in Java

    If you look at the pattern class you will see that those \ (escape) characters must be escaped themselves, i.e. preced by ANOTHER \, so \\d not \d.

    Edit: And \\s, not \\p is whitespace, and if it should be "optional" then you need to place either an * (if it can encompass multiple whitespace characters) or a ? if it is either no or a single whitespace character after it. And don't use the - in your bracketed part that is, at present saying c or any character ranging from f to C or F is acceptable in that spot.
    Last edited by masijade; 01-17-2014 at 12:53 PM.

  6. #6
    mallorz is offline Member
    Join Date
    Jan 2014
    Posts
    68
    Rep Power
    0

    Default Re: Simple Regular Expression - first time in Java

    Okay, so then I changed it to : \\d{1,3}\\p?[cf, CF]

    I've played around with the alphabetical charcters because I can't seem to get a target string to match. I tested the following 5 inputs: 43c (should match), 109 f (should match), 58C (should match), 90d (no match), gj# (no match).

    I have to head out to work. I may pop in throughout the day since I can test from another website, that way I can dive right back into my code when I get home.

    I appreciate all of the help.

  7. #7
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: Simple Regular Expression - first time in Java

    \\s is whitespace NOT \\p

  8. #8
    mallorz is offline Member
    Join Date
    Jan 2014
    Posts
    68
    Rep Power
    0

    Default Re: Simple Regular Expression - first time in Java

    Quote Originally Posted by masijade View Post
    \\s is whitespace NOT \\p
    ...as you said. Sorry about that. That's what I get for rushing at 5:30 am before work. I will see how it works with the correction. Thank you.

  9. #9
    mallorz is offline Member
    Join Date
    Jan 2014
    Posts
    68
    Rep Power
    0

    Default Re: Simple Regular Expression - first time in Java

    Masijade,

    It works! Thank you again for your patience and assistance. I really do appreciate it.

  10. #10
    mallorz is offline Member
    Join Date
    Jan 2014
    Posts
    68
    Rep Power
    0

    Default Re: Simple Regular Expression - first time in Java

    Alright ... So I added this to my code and it is generating the following error: Syntax error on tokens. Delete these tokens.

    Specifically, the following are the parts underlined in red:
    //d{
    3\\s?[cf
    CF]

    Code:
    Java Code:
    			String input = JOptionPane.showInputDialog("Enter a temperature value (digit followed by C/F)");
    			
    			// Analyze input
    			 if (!input.matches(\\d{1,3}\\s?[cf, CF])){
    				input = JOptionPane.showInputDialog("Enter a temperature value (digit followed by C/F)");
    			}
    I did add Java.util.regex to the beginning of my project.


    EDIT: Got'rr. Needed quotation marks. :)
    Last edited by mallorz; 01-18-2014 at 02:15 AM.

  11. #11
    mallorz is offline Member
    Join Date
    Jan 2014
    Posts
    68
    Rep Power
    0

    Default Re: Simple Regular Expression - first time in Java

    Okay, so further testing reveals something is not right. For example, 84g shows as valid but 84H does not - neither should be - only C and F.

    Also, if the user enter a lowercase value I want to replace it with an uppercase. The code below does not work. I'd also like to remove any spaces for conformity purposes (assignment requires to allow them in initial input).

    Could someone please tell me what I am missing with all of this? I haven't been able to figure these out yet and these are the last things I need to solve.


    // Convert any lower case characters to upper case
    if (input.contains("c")){
    input.replace("c", "C");
    }
    else if (input.contains("f")){
    input.replace("f", "F");
    }
    Last edited by mallorz; 01-18-2014 at 02:19 AM.

  12. #12
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default Re: Simple Regular Expression - first time in Java


  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,508
    Blog Entries
    7
    Rep Power
    20

    Default Re: Simple Regular Expression - first time in Java

    Change the selection part of the RE to [CFcf] (no minus sign anywhere because that introduces a range); also put the entire thing between double quotes because we'red dealing with a String representation of the RE.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  14. #14
    mallorz is offline Member
    Join Date
    Jan 2014
    Posts
    68
    Rep Power
    0

    Default Re: Simple Regular Expression - first time in Java

    I sincerely apologize - This is my very first Java program, so I appreciate your patience. I don't understand what I am doing wrong. I got the string alterations figured out, however the RegEx still allows 34g for example.

    Java Code:
    if (!input.matches("\\d{1,3}\\s?[cfCF]")){
    				JOptionPane.showMessageDialog(null, "Invalid input.");
    				input = JOptionPane.showInputDialog("Enter a temperature value (digit followed by C/F)");
    			}
    			input = input.toUpperCase();
    			input = input.replaceAll("\\W", "");
    Last edited by mallorz; 01-18-2014 at 02:07 PM.

  15. #15
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,508
    Blog Entries
    7
    Rep Power
    20

    Default Re: Simple Regular Expression - first time in Java

    How strange; if I do this:

    Java Code:
    		String input="34g";
    		System.out.println(input.matches("\\d{1,3}\\s?[cfCF]"));
    the code fragment prints 'false' (as it should).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  16. #16
    mallorz is offline Member
    Join Date
    Jan 2014
    Posts
    68
    Rep Power
    0

    Default Re: Simple Regular Expression - first time in Java

    Really? Thank you for running a test on your end, I guess I'll send my program over to my instructor later.

    Much appreciated!

  17. #17
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,508
    Blog Entries
    7
    Rep Power
    20

    Default Re: Simple Regular Expression - first time in Java

    Quote Originally Posted by mallorz View Post
    Really? Thank you for running a test on your end, I guess I'll send my program over to my instructor later.

    Much appreciated!
    What happened when you ran my little test?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  18. #18
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Simple Regular Expression - first time in Java

    Get in the habit of using standard Java naming conventions!

  19. #19
    mallorz is offline Member
    Join Date
    Jan 2014
    Posts
    68
    Rep Power
    0

    Default Re: Simple Regular Expression - first time in Java

    Quote Originally Posted by JosAH View Post
    How strange; if I do this:

    Java Code:
    		String input="34g";
    		System.out.println(input.matches("\\d{1,3}\\s?[cfCF]"));
    the code fragment prints 'false' (as it should).

    kind regards,

    Jos
    It returns false.

    Perhaps stranger: I just entered 43g repeatedly - sometimes it was accepted other times it was rejected. I sent my full program to my instructor. If you'd like, I'll let you know what I figure out. Hopefully she finds something.
    Last edited by mallorz; 01-18-2014 at 06:37 PM.

  20. #20
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,595
    Rep Power
    5

    Default Re: Simple Regular Expression - first time in Java

    There must be something else you are doing to cause such erratic behavior. The problem is probably elsewhere. Just before you do you test, have you printed out the input value you are trying to match on success or failure? This might ensure you are actually matching the value you think you are.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Page 1 of 2 12 LastLast

Similar Threads

  1. A Simple Regular Expression...
    By mrashi12 in forum New To Java
    Replies: 10
    Last Post: 01-30-2012, 12:52 AM
  2. Java Help Regular Expression
    By geforce in forum New To Java
    Replies: 13
    Last Post: 01-28-2012, 08:29 AM
  3. URGENT HELP : Java Regular Expression
    By asheshrocky in forum Advanced Java
    Replies: 3
    Last Post: 12-07-2011, 02:52 PM
  4. Java Regular expression ?
    By sidharth in forum Advanced Java
    Replies: 12
    Last Post: 11-14-2009, 11:09 AM
  5. Java Regular Expression help
    By royalibrahim in forum Advanced Java
    Replies: 11
    Last Post: 11-12-2009, 01:27 AM

Tags for this Thread

Posting Permissions

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