Regular Expression Groups
===========
Sample Code
===========
string testIpString = "192.168.110.112";
string regularExpressionString = @"^(?<octet1>[01]?\d\d2[0-4]\d25[0-5])(\.)(?<octet2>[01]?\d\d2[0-4]\d25[0-5])(\.)(?<octet3>[01]?\d\d2[0-4]\d25[0-5])(\.)(?<octet4>[01]?\d\d2[0-4]\d25[0-5])$";
//Option 1//Match matches = Regex.Match(testIpString, regularExpressionString);
//Option 2//Match matches = Regex.Match(testIpString, regularExpressionString,RegexOptions.ExplicitCapture);
Console.WriteLine("Groups");
foreach (Group group in matches.Groups)
{
Console.WriteLine(group.ToString());
}
Console.WriteLine("Named Groups");
Console.WriteLine( matches.Groups["octet1"].ToString());
Console.WriteLine( matches.Groups["octet2"].ToString());
Console.WriteLine( matches.Groups["octet3"].ToString());
Console.WriteLine( matches.Groups["octet4"].ToString());
Console.ReadLine();
===============
Option 1 output
===============
Groups
192.168.110.112
.
.
.
192
168
110
112
Named Groups
192
168
110
112
===============
Option 2 output
===============
Groups
192.168.110.112
192
168
110
112
Named Groups
192
168
110
112
===============
Commentary
===============
Most of the power is in the regular expression:
@"^(?<octet1>[01]?\d\d2[0-4]\d25[0-5])(\.)(?<octet2>[01]?\d\d2[0-4]\d25[0-5])(\.)(?<octet3>[01]?\d\d2[0-4]\d25[0-5])(\.)(?<octet4>[01]?\d\d2[0-4]\d25[0-5])$";
Showing posts with label Regular Expressions. Show all posts
Showing posts with label Regular Expressions. Show all posts
Tuesday, July 1, 2008
Friday, June 27, 2008
Regular Expressions: Special Characters And Escape Sequences
Regular Expressions: Special Characters And Escape Sequences
Special Characters
"^" Beginning of text
"$" End of text
"." Any single character but (\n) the newline
"*" Previous expression 0 or more times
"+" Preceding character 1 or more times
"?" Preceding character 0 or 1 times
"" Alteration used in Groups
"(" Used in Groups
")" Used in Groups
"[" Used in Ranges
"]" Used in Ranges
"-" Used in Groups and Ranges
"{" Used in Quantifiers
"}" Used in Quantifiers
Escape Sequences
"\b" Word boundary
"\B" Any non Word boundary
"\d" Any digit
"\D" Any non digit
"\s" Any whitespace character; space, tab, newline
"\S" Any character that is not whitespace
Special Characters
"^" Beginning of text
"$" End of text
"." Any single character but (\n) the newline
"*" Previous expression 0 or more times
"+" Preceding character 1 or more times
"?" Preceding character 0 or 1 times
"" Alteration used in Groups
"(" Used in Groups
")" Used in Groups
"[" Used in Ranges
"]" Used in Ranges
"-" Used in Groups and Ranges
"{" Used in Quantifiers
"}" Used in Quantifiers
Escape Sequences
"\b" Word boundary
"\B" Any non Word boundary
"\d" Any digit
"\D" Any non digit
"\s" Any whitespace character; space, tab, newline
"\S" Any character that is not whitespace
Labels:
Code,
Groups,
Quantifiers,
Ranges,
Regex,
Regular Expressions
Tuesday, June 24, 2008
Regex Characters Digits and Underscores
Regex to validate a string is composed of only characters, numbers and underscores.
==comments==
Brackets mean match any one of:
"[abcde]" means match any one of a,b,c,d,e.
Dash between characters is a short hand for include all characters between.
"[a-z]" any lowercase letter
"[A-Z]" any uppercase letter
"[0-9]" any digit
Plus sign after a bracket means match one or more
"[a-zA-Z_0-9]+" will match any substring composed of only letter, digits or the underscore, the substring must have at least one
Dollar Sign matches the beginning of a string and Hat '^' matches the end of a string.
"$[a-zA-Z_0-9]+^" will match any substring composed of only letter, digits or the underscore, the substring must have at least one. Also there can be no characters before or after the matching substring.
==code==
public bool ValidateIdentifier(string identifier)
{
Regex re = new Regex("^[a-zA-Z_0-9]+$");
MatchCollection match = re.Matches(identifier);
if (match.Count != 1)
{
//_LastException = new Exception("Identifier must have only characters, digits and underscores");
return false;
}
return true;
}
==comments==
Brackets mean match any one of:
"[abcde]" means match any one of a,b,c,d,e.
Dash between characters is a short hand for include all characters between.
"[a-z]" any lowercase letter
"[A-Z]" any uppercase letter
"[0-9]" any digit
Plus sign after a bracket means match one or more
"[a-zA-Z_0-9]+" will match any substring composed of only letter, digits or the underscore, the substring must have at least one
Dollar Sign matches the beginning of a string and Hat '^' matches the end of a string.
"$[a-zA-Z_0-9]+^" will match any substring composed of only letter, digits or the underscore, the substring must have at least one. Also there can be no characters before or after the matching substring.
==code==
public bool ValidateIdentifier(string identifier)
{
Regex re = new Regex("^[a-zA-Z_0-9]+$");
MatchCollection match = re.Matches(identifier);
if (match.Count != 1)
{
//_LastException = new Exception("Identifier must have only characters, digits and underscores");
return false;
}
return true;
}
Subscribe to:
Posts (Atom)