Regular Expression – II

//Matches string start with pattern
String strData = “Hello there”;
Regex rg = new Regex(“^Hello”);
bool truefalse = rg.IsMatch(strData);

rg = new Regex(“^Hola”);
truefalse = rg.IsMatch(strData);

//Matches strung end with pattern
strData = “Hello there”;
rg = new Regex(“there$”);
truefalse = rg.IsMatch(strData);

//Matches char from the list
rg = new Regex(“[aeiou]”);
truefalse = rg.IsMatch(strData);

//Not matches char from the list
rg = new Regex(“[^aeiou]”);
truefalse = rg.IsMatch(strData);

//Matching range
strData = “Hello there, you are 8 years old.”;
rg = new Regex(“[0-9]”);
truefalse = rg.IsMatch(strData);

strData = “55”;
rg = new Regex(“[0-9][0-5]”);
truefalse = rg.IsMatch(strData);

//Match string contains A-Z, a-z and 0-9
strData = “Hello”;
rg = new Regex(“[^a-zA-Z0-9]”);
truefalse = rg.IsMatch(strData);

//Match string contains A-Z, a-z, 0-9 with white space, below code return false when string contains chars other than defined.
strData = “hello there 0″;
rg = new Regex(@”[^a-zA-Z0-9\s]”);
truefalse = rg.IsMatch(strData);

//Matches a-z and A-Z
strData = “hello there”;
rg = new Regex(@”[^\w\s]”);
truefalse = rg.IsMatch(strData);

//Matches a-z, A-Z and 0-9
strData = “hello there 0″;
rg = new Regex(@”[^\w\d\s]”);
truefalse = rg.IsMatch(strData);

//Note*: \w represet a-zA-Z, \W other than word characters and \d represent 0-9, \D other than non-decimal
//\s represent whitespace

//To match the number of characters that matches certain pattern.

//True scenario
strData = “12345”;
rg = new Regex(@”\d{5}”);
truefalse = rg.IsMatch(strData);

strData = “12345-6789″;
rg = new Regex(@”\d{5}-\d{4}”);
truefalse = rg.IsMatch(strData);

//False scenario
strData = “12345-678″;
rg = new Regex(@”\d{5}-\d{4}”);
truefalse = rg.IsMatch(strData);

strData = “12345-678A”;
rg = new Regex(@”\d{5}-\d{4}”);
truefalse = rg.IsMatch(strData);

//Minimum and Maximum Occurences
//True scenario
strData = “123”;
rg = new Regex(@”\d{1,3}”);
truefalse = rg.IsMatch(strData);

strData = “12”;
rg = new Regex(@”\d{1,3}”);
truefalse = rg.IsMatch(strData);

//False scenario – string end with numeric
strData = “A124B”;
rg = new Regex(@”\d{1,3}$”);
truefalse = rg.IsMatch(strData);

#regular-expression

Regular Expression – I

Regular expression are often used to make sure that a string matches a certain pattern. For example, that a string looks like a zip code, or phone number, or e-mail address.

The simplest regular expression is just a sub-string. For example, the regular expression ther matches the string hello there, because the string contains the regular expression.

If you’re familiar with JavaScript regular expression, then you’ll you already know most of this. .NET regular expression are just a super set of JavaScript regular expression.

Start and End of Line

You can easily match strings that start or end with certain characters. The ^ character matched the start of the string. For example:

^hello Matches hello there, hello Shaikh, hellotopical

To match the end
of the string, use the $ character. For example:

ere$
Matches Where, and There

The ^
and $ characters are know as “Atomic Zero Width Assertions”,
in case you were wondering.

Character Classes

Character classes allow you to specify sets of characters or ranges. For example:

[aeiou]
Matches Hey, and Hi, but not Zzz

In other words,
the string must contain at least one of the characters in the character class.
You can also exclude characters. For example:

[^aeiou]
Matches Zzz, but not Hey or Hi.

When the ^
character is the first character in the character class, it means “anything but the following characters”.

Putting this together, we could create a pattern that matches strings that start with a vowel:

[aeiou] Or, strings that don’t start with a vowel: [^aeiou]

With character classes, you can also specify ranges. For example:

[0-9]
Matches 0, 5, 8, or any number between 0 and 9.

[0-9][0-9]
Matches any two digit number (04, 13, 87, etc.), but there’s a better way to do this.

#regular-expression