Tutorial by Examples

You should remember that regex was designed for matching a date (or not). Saying that a date is valid is a much more complicated struggle, since it will require a lot of exception handling (see leap year conditions). Let's start by matching the month (1 - 12) with an optional leading 0: 0?[1-9]|1[...
Matching an email address within a string is a hard task, because the specification defining it, the RFC2822, is complex making it hard to implement as a regex. For more details why it is not a good idea to match an email with a regex, please refer to the antipattern example when not to use a ...
Here's how to match a prefix code (a + or (00), then a number from 1 to 1939, with an optional space): This doesn't look for a valid prefix but something that might be a prefix. See the full list of prefixes (?:00|\+)?[0-9]{4} Then, as the entire phone number length is, at most, 15, we can look...
IPv4 To match IPv4 address format, you need to check for numbers [0-9]{1,3} three times {3} separated by periods \. and ending with another number. ^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$ This regular expression is too simple - if you want to it to be accurate, you need to check that the numbers are be...
For a 12hour time format one can use: ^(?:0?[0-9]|1[0-2])[-:][0-5][0-9]\s*[ap]m$ Where (?:0?[0-9]|1[0-2]) is the hour [-:] is the separator, which can be adjusted to fit your need [0-5][0-9] is the minute \s*[ap]m followed any number of whitespace characters, and am or pm If you need th...
Regex to match postcodes in UK The format is as follows, where A signifies a letter and 9 a digit: FormatCoverageExampleCellCellAA9A 9AAWC postcode area; EC1–EC4, NW1W, SE1P, SW1EC1A 1BBA9A 9AAE1W, N1C, N1PW1A 0AXA9 9AA, A99 9AAB, E, G, L, M, N, S, WM1 1AE, B33 8THAA9 9AA, AA99 9AAAll other postco...

Page 1 of 1