Regex - Time

  • Thread starter Thread starter Ron
  • Start date Start date
R

Ron

Hi,

Can anyone help out with a regex for time?

I would like the regex to accept:

HH:MM AM
HH:MM PM

Optional space, optional 1st H, case doesn't matter. Thanks in advance.
 
Hi Ron,

Try this one:

(?<Time>^(?:0?[1-9]:[0-5]|1(?=[012])\d:[0-5])\d(?:[ap]m)?)See you,Roberto
 
Thanks for the quick reply Roberto.

Am I supposed to include the whole string, starting w/:

"(?<Time>^........[ap],?)"

When I do include it all, the browser gets a "Syntax Error in Regular
Expression" and it doesn't work.

When I remove the "(?<Time>" from the beg. and one ) from the end, the
expression accepts 1:00 w/o the PM, which is not ok. Am am I doing
something wrong? Thanks again.
 
Hi Ron,

It's pretty strange, because I tryed befor send you. Try do it like this:

Dim regxInteger As New
System.Text.RegularExpressions.Regex("(?<Time>^(?:0?[1-9]:[0-5]|1(?=[012])\d
:[0-5])\d(?:[ap]m)?)")

MessageBox.Show(regxInteger.IsMatch("12:0aAM").ToString)

I hope it works.

See you,

Roberto Lopes

Roberto Lopes said:
Hi Ron,

Try this one:

(?<Time>^(?:0?[1-9]:[0-5]|1(?=[012])\d:[0-5])\d(?:[ap]m)?)See you,Roberto
Lopes"Ron said:
Hi,

Can anyone help out with a regex for time?

I would like the regex to accept:

HH:MM AM
HH:MM PM

Optional space, optional 1st H, case doesn't matter. Thanks in advance.
 
Ron,

Realize that the example I sent you will return false, because the time is
wrong, ok?

Try testing a valid time like this:

Dim regxInteger As New
System.Text.RegularExpressions.Regex("(?<Time>^(?:0?[1-9]:[0-5]|1(?=[012])\d
:[0-5])\d(?:[ap]m)?)")

MessageBox.Show(regxInteger.IsMatch("12:00 AM").ToString)

Ok?

Thanks,

Roberto Lopes
 
Hi again,

I got these examples to work, however the expression accepts a time w/o
the AM or PM. AM/PM must be included, meaning '1:00' should return
false.

I also keep getting the bad syntax error when I tab out of the validated
txtbox.
 
Hi Ron,

I didn't know it was mandadory. Try this one:
Dim regxInteger As New
System.Text.RegularExpressions.Regex("(^(?:0?[1-9]:[0-5]|1(?=[012])\d:[0-5])
\d( )?(AM|am|aM|Am|PM|pm|pM|Pm))$")

Write back telling if it worked fine, ok?

See you.

Roberto Lopes
 
That seems to be working great!!!

I noticed you took out '<time>'. Is that what was causing the syntax
errors (which I no longer get)?

Also, any suggestions on where I can get a listing of the symbols and
how they are used? Thank you very much.

--Ron
 
Back
Top