Regex
regex
January 10, 20233 min read

Regex for time am/pm

Time is a concept that is used to describe the duration of events or the progression of existence. It is often measured in units such as seconds, minutes, hours, days, and years. In this article let's understand how we can create a regex for time in am/pm and how regex can be matched for a given time.

Regex (short for regular expression) is a powerful tool used for searching and manipulating text. It is composed of a sequence of characters that define a search pattern. Regex can be used to find patterns in large amounts of text, validate user input, and manipulate strings. It is widely used in programming languages, text editors, and command line tools.

Structure of a time - hh:mm am/pm

The time should have the following criteria and structure-

  • It should start from 1, 2, ... 9 or 10, 11, 12.
  • It should be followed by a colon(:).
  • It should be followed by two digits between 00 to 59.
  • It should only allow one white space, although this is optional.
  • It should end with 'am', 'pm or 'AM', 'PM'.

Regex for checking if time hh:mm am/pm is valid or not

Regular Expression for time hh:mm am/pm is-

/^(1[012]|[1-9]):[0-5][0-9](\\s)?(?i)(am|pm)$/gm

Test string examples for the above regex-

Input StringMatch Output
12:43does not match
32:11does not match
10:10ammatches
12:44pmmatches
2:12ammatches

Here is a detailed explanation of the above regex-

/^(1[012]|[1-9]):[0-5][0-9](\\s)?(?i)(am|pm)$/gm

^ asserts position at start of a line
1st Capturing Group (1[012]|[1-9])
1st Alternative 1[012]
1 matches the character 1 with index 4910 (3116 or 618) literally (case sensitive)
Match a single character present in the list below [012]
012 matches a single character in the list 012 (case sensitive)
2nd Alternative [1-9]
Match a single character present in the list below [1-9]
1-9 matches a single character in the range between 1 (index 49) and 9 (index 57) (case sensitive)
: matches the character : with index 5810 (3A16 or 728) literally (case sensitive)
Match a single character present in the list below [0-5]
0-5 matches a single character in the range between 0 (index 48) and 5 (index 53) (case sensitive)
Match a single character present in the list below [0-9]
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
2nd Capturing Group (\\s)?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
\\ matches the character \ with index 9210 (5C16 or 1348) literally (case sensitive)
s matches the character s with index 11510 (7316 or 1638) literally (case sensitive)
(?i) match the remainder of the pattern with the following effective flags: gmi
i modifier: insensitive. Case insensitive match (ignores case of [a-zA-Z])
3rd Capturing Group (am|pm)
1st Alternative am
am matches the characters am literally (case insensitive)
2nd Alternative pm
pm matches the characters pm literally (case insensitive)
$ asserts position at the end of a line
Global pattern flags
g modifier: global. All matches (don't return after first match)
m modifier: multi line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)

Hope this article was useful to check if the string is a valid time am/pm or not.

Share this blog
Tagged in :
regex
Like what you read?
Subscribe to our Newsletter
Subscribe to our email newsletter and unlock access to members-only content and exclusive updates.
About the Author
Satvik
Satvik
Entrepreneur
Satvik is a passionate developer turned Entrepreneur. He is fascinated by JavaScript, Operating System, Deep Learning, AR/VR. He has published several research papers and applied for patents in the field as well. Satvik is a speaker in conferences, meetups talking about Artificial Intelligence, JavaScript and related subjects. His goal is to solve complex problems that people face with automation. Related projects can be seen at - [Projects](/projects)
View all articles
Previous Article
Next Article
January 10, 20233 min read