Search code examples
javaregexstringtimejsoup

Check if time exists in a Java string


I have a String variable which consists of some text with time in it. example,

// string str looks like this
String str = "XYZ SuperMarket 10.00AM - 10.00PM"
  1. Is there a way or lib which I can use to check if this string
    consists of time(Something like Jsoup or Regex)?

  2. I want to trim the time content & store it in a different String named "time". Is it really possible to process strings this way.


Solution

  • I'm have no knowledge in Java programming and therefore cannot offer a Java code for this task.

    But I can offer following regular expression:

    ((?:[01][0-9]|[0-9])(?:[.:][0-5][0-9]){0,2} ?[AP]M)
    

    (...) at beginning and end of the entire expression mark the entire found string which might be needed to get the found string into a string variable.

    (?:[01][0-9]|[0-9]) is a non marking group for an OR expression. This OR expression finds either a number with 2 digits in range 00 to 19 or only a single digit in range 0 to 9. (Yes, numbers greater than 12 are also matched, but does that matter? I suppose, it does not. Keep it simple means make it fast.)

    (?:[.:][0-5][0-9]){0,2} is a non marking group which is applied on the string to search 0, 1 or 2 times. It matches the minute and the second with the separator character between which can be either a dot or a colon. The minute/second must have 2 digits in range 00 to 59. (So this expression does not work for a leap second with has value 60, but I'm quite sure you have never seen a time with a leap second.) So the time string can be with just the hour, or with hour + minute, or with hour + minute + second. As I have never seen a time string with a single digit minute or second after an hour, the limitation on 2 digits minutes/seconds should not be a problem.

     ? matches a single space which could, but most not exist in time string.

    [AP]M matches AM or PM as it looks like this is the time format you expect in the search string.

    Note: There are dozens of different time formats. Therefore checking a string for all possible time formats would required lots of regular expressions.