Home > database >  Regular expression matching timezone label with offset
Regular expression matching timezone label with offset

Time:02-22

I rarely use regular expression, now I want to use regular expression in validating a timezone entry. These entries are saved in DB and only the offset value is used.

Below are the valid entries:

Australian Central Time ( GMT 10:30 )
London - Greenwich Mean Time ( GMT 00:00 )
Beijing, Hong Kong (GMT 08:00)
Japan Standard Time (GMT 09:00)
Pacific Daylight Time (GMT-07:00 )
Pacific Standard Time (GMT-08:00)
Singapore Time( GMT 08:00)
Europe Time  1 (GMT 01:00)

What I want is to check that the entry format is something like: Any words and characters here like above( GMT 08:00). GMT, -/ , 2 digits hours, :, 2 digits minutes are fixed. ( and ) can have spaces.

Below are invalid entries:

Europe Time GMT 01:00
GMT 02:00
(GMT 01:30)
Hello
Philippines
Singapore Time (GMT   08:00)
Singapore Time (GMT 8:0)
Singapore Time (UTC 08:00)

This is what I tried, but it is far from what I wanted: ^[\w\s\ ,-] [\(\s] [GMT] [-\ ] [\d{2}] [:] [\d{2}] [\)\s]

Thanks to @sleepwalk for his answer, I just tweak it a little to fit to my needs.

. \(\s*GMT [ -] \d{1,2}:\d{2}\s*\)\s*

CodePudding user response:

I think this is what you want

.*\(\s*GMT\s*[ -]?\s*\d{1,2}:\d{2}\s*\)\s*

You can test your regexs here https://regexr.com/

  • Related