From: Robert Klemme Date: 2008-08-13T18:40:12+09:00 Subject: Re: regular expresson isue 2008/8/13 Raj Singh : > I have following regular expression to parse time. > > ([0-9]|0[0-9]|1[012]):([0-9]|0[0-9]|[1-5][0-9])\s*(am|pm) This seems overly complex, especially your minute matching. This can be simplified at least to "\d?\d" or "\d{1,2}". > I don't want this regext to match 13:45 PM because the the numerical > value before the colon should be limited to 12. How to fix it? Do you need this for direct matching of a string or scanning of larger volumes of text? Here's an alternative (untested): %r{ \b (0?\d|1[012]) # hour : ([0-5]\d) # minute \s* (am|pm) }xi If you want to use this for matching you probably rather want %r{ \A (0?\d|1[012]) # hour : ([0-5]\d) # minute \s* (am|pm) \z }xi Note, the comments can stay in there when copy and pasting. Kind regards robert -- use.inject do |as, often| as.you_can - without end