Friday, 15 January 2010

c# - Regex - including the colon -



c# - Regex - including the colon -

i utilize regex match first twelve characters of string i've received. i'm receiving string , want validate string discard , keep. 1 time i've validated string instantiate object based on info in string.

in illustration want check specific character (a), 8 numbers, colon , either b|c followed d. pattern identifies string work with. next pattern fails match , suspect due colon

if(regex.ismatch(my_string,"a[0-9]{8,}:(b|c)d"))

i want match, zeroes number 0-9 , b interchangeable c. need verify colon present, there cases string may malformed.

example of characters should pass regex pattern;

a00000000:bd

that regex should work. few suggestions:

{8,} matches 8 , more characters. the entire regex match substrings of longer string (i.e. "xyza12345678:cdefg"). if don't want that, anchor regex. (b|c) can replaced [bc]

so seek this:

if (regex.ismatch(my_string,"^a[0-9]{8}:[bc]d"))

c# regex visual-studio-2008 .net-3.5

No comments:

Post a Comment