Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Regular Expressions

Regular Expressions

Scheduled Pinned Locked Moved C#
regexhelpquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mikasa
    wrote on last edited by
    #1

    Ok, I am trying to make an Expression to evaluate a Short Time format (hh:mm). I've seen the standard format of "\d\d:\d\d" but I can't use it because it allows values greater than 23 for the Hour. I've come up with one but I need help on the 2nd digit in the hour. Basically in laymen's terms it goes something like this: (1 or 2)(??):(0 - 5)(0 - 9). The Regular Expression looks like this: "[1,2](Nothing here??):[0-5]\d". Here's what I need to do. If the first Digit is a "1", then the 2nd digit can be "\d" or "[0-9]" (for hours 0 - 19) . Otherwise, if the first digit is "2", then the 2nd digit can only be "[0-3]" (for hours to 20 - 23). Can anyone help?

    Richard DeemingR A 2 Replies Last reply
    0
    • M mikasa

      Ok, I am trying to make an Expression to evaluate a Short Time format (hh:mm). I've seen the standard format of "\d\d:\d\d" but I can't use it because it allows values greater than 23 for the Hour. I've come up with one but I need help on the 2nd digit in the hour. Basically in laymen's terms it goes something like this: (1 or 2)(??):(0 - 5)(0 - 9). The Regular Expression looks like this: "[1,2](Nothing here??):[0-5]\d". Here's what I need to do. If the first Digit is a "1", then the 2nd digit can be "\d" or "[0-9]" (for hours 0 - 19) . Otherwise, if the first digit is "2", then the 2nd digit can only be "[0-3]" (for hours to 20 - 23). Can anyone help?

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      Try this:

      ^(?<hour>\d|([0-1]\d)|(2[0-3])):(?<min>[0-5]\d)$


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      1 Reply Last reply
      0
      • M mikasa

        Ok, I am trying to make an Expression to evaluate a Short Time format (hh:mm). I've seen the standard format of "\d\d:\d\d" but I can't use it because it allows values greater than 23 for the Hour. I've come up with one but I need help on the 2nd digit in the hour. Basically in laymen's terms it goes something like this: (1 or 2)(??):(0 - 5)(0 - 9). The Regular Expression looks like this: "[1,2](Nothing here??):[0-5]\d". Here's what I need to do. If the first Digit is a "1", then the 2nd digit can be "\d" or "[0-9]" (for hours 0 - 19) . Otherwise, if the first digit is "2", then the 2nd digit can only be "[0-3]" (for hours to 20 - 23). Can anyone help?

        A Offline
        A Offline
        Arun Bhalla
        wrote on last edited by
        #3

        If you really want to do all the error checking in the regular expression for Short Time format, try this: ^(([01]?\d)|(2[0-3])):[0-5]\d$ Or more explicitly: ^(([01]?[0-9])|([2][0-3])):([0-5][0-9])$ If you don't want to allow h:mm times, take out the ?.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups