Showing posts with label Regex. Show all posts
Showing posts with label Regex. Show all posts

Tuesday, October 27, 2009

British date time regular expression dd/mm/yyyy

I came up with the following before which is not entirely correct.

^\d{2}/(\d{2})/(\d{4})+


Why? You can enter the date as 45\23\0088. This is just digit matching.

thus, i refactor to the following:


((0{1}[1-9])|([1-2][0-9])|(3{1}[0-1]))/((0{1}[1-9])|(1{1}[0-2]))/((19|20)\d\d)


This regex only allow you to enter
dd from 01-31.
mm from 01-09 and 11-12
yyyy range restricted to 19xx-20xx.


How to get it tested? Try the following flash regex engine:
http://gskinner.com/RegExr/



Thursday, June 25, 2009

Regular Expression to search for time duration

How to find the following time string in a text file?
(09:00 -12:00)

Try regular expression:
\((\d{1,2}:\d{1,2}.*\d{1,2}:\w*)\)

Imagine you are trying to remove the time from this sting "meet up with friends (09:00 - 12:00)"

Code:
string result = System.Text.RegularExpressions.Regex.Replace("meet up with friends (09:00 - 12:00)", @"\((\d{1,2}:\d{1,2}.*\d{1,2}:\w*)\)",string.Empty)

Result:
result = "meet up with friends" is produced.

Monday, March 30, 2009

Free Regular Expression Designer

Check out this unicode regex editor from RadSoftware. Came across this in Microsoft TechNet April 2009.

Alternatively, this is the one I personally prefer -> Expresso Regex editor (version 3.0)

Monday, August 06, 2007

Regular Expression to validate number string

What if you want to validate a string with a floating point number without any other characters in it except minus (-) sign. You can use multiple if/switch statement but the code goes long way down
to cater for all invalid characters (such as * & $ £).

Regular Expression comes to the rescue. Lets take the following example.

e.g
Valid numbers
0.45, -0.33, -34, 100, 0, -3.44

Invalid numbers
$3, +6.55, --56, 46*, 33"!, &, +88

Regular Expression = (^-?\d{1,3}\.?\d{0,5}?$)

Explanations:

^-? == ^ sign means match the string starting with. ? (question mark) means optional. The string means match the string that starts with an optional minus sign. This mean string starts with "-2" and "2" are valid. "+2", "@%£2" are invalid.