Results 1 to 4 of 4
  1. #1
    milky is offline Member
    Join Date
    Jun 2016
    Posts
    4
    Rep Power
    0

    Default Why is my Regular Expression not working?

    Hello, I'm trying to use a Regular Expression to check a time format, Im getting a syntax error when i try to declare the String regEx. The error is highlighted under "\d". Why is it not working?

    Java Code:
    public static boolean timeError(String time){
    		time = time.replaceAll("\\s+","");
    		String regexStr =  "\d[/U][/B]+D(\d|[01]\d|2[0-4])H(\d|[0-5]\d|60)M(\d|[0-5]\d|60)S";//RegEX for date format
    		if(time.matches(regexStr) ){
    			return true;
    		}
    		else{
    			System.out.println("Not a valid time format");
    			return false;
    		}
    		
    	}

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

    Default Re: Why is my Regular Expression not working?

    That literal String first needs to be compiled by the Java compiler and it doesn't like the escaped character '\d'; the remedy is simple though: change the '\d' sequences to '\\d'; the Java compiler will strip off one '\' and passes '\d' to the regular expression compiler.

    kind regards,

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

  3. #3
    milky is offline Member
    Join Date
    Jun 2016
    Posts
    4
    Rep Power
    0

    Default Re: Why is my Regular Expression not working?

    I was able to solve it, Thank you sir!
    Java programmers Unite!
    Last edited by milky; 06-20-2016 at 05:36 PM.

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

    Default Re: Why is my Regular Expression not working?

    Have you looked at the various Date and Time formatter classes? There are numerous methods available for formatting
    dates and times. Also, check out the LocalTime class (Java 8). Other similar classes are available pre-Java 8.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Regular Expression
    By allaudin in forum New To Java
    Replies: 8
    Last Post: 10-02-2013, 09:26 AM
  2. Regular expression
    By garnaout in forum New To Java
    Replies: 4
    Last Post: 05-15-2012, 06:22 PM
  3. Regular Expression Help
    By niketanand in forum Advanced Java
    Replies: 1
    Last Post: 06-24-2011, 04:56 PM
  4. regular expression
    By ehsansad in forum New To Java
    Replies: 8
    Last Post: 03-18-2011, 02:23 PM
  5. regular expression
    By prof.deedee in forum JDBC
    Replies: 3
    Last Post: 02-19-2010, 11:15 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
  •