Csharp/C Sharp/Development Class/Matcher
Содержание
Define multiline patterns
<source lang="csharp"> using System; using System.Text.RegularExpressions; public class EntryPoint {
static void Main( string[] args ) { // Create regex to search for IP address pattern. string pattern = @"
- First part match
([01]?\d\d?
- OR
|2[0-4]\d
- OR
|25[0-5])
\.
- REPEAT
([01]?\d\d?|2[0-4]\d|25[0-5])\.
- REPEAT
([01]?\d\d?|2[0-4]\d|25[0-5])\.
- REPEAT
([01]?\d\d?|2[0-4]\d|25[0-5]) ";
Regex regex = new Regex( pattern, RegexOptions.IgnorePatternWhitespace ); Match match = regex.Match( "192.168.169.1" ); while( match.Success ) { Console.WriteLine( "IP Address found at {0} with " + "value of {1}", match.Index, match.Value ); match = match.NextMatch(); } }
}
</source>
Is Match successful
<source lang="csharp"> using System; using System.Text.RegularExpressions; public class EntryPoint {
static void Main( string[] args ) { // Create regex to search for IP address pattern. string pattern = @"([01]?\d\d?|2[0-4]\d|25[0-5])\." + @"([01]?\d\d?|2[0-4]\d|25[0-5])\." + @"([01]?\d\d?|2[0-4]\d|25[0-5])\." + @"([01]?\d\d?|2[0-4]\d|25[0-5])"; Regex regex = new Regex( pattern ); Match match = regex.Match( "192.168.169.1" ); while( match.Success ) { Console.WriteLine( "IP Address found at {0} with " + "value of {1}", match.Index, match.Value ); Console.WriteLine( "Groups are:" ); foreach( Group g in match.Groups ) { Console.WriteLine( "\t{0} at {1}", g.Value, g.Index ); } match = match.NextMatch(); } }
}
</source>
MatchEvaluator: Entry Point IP Reverse
<source lang="csharp"> using System; using System.Text; using System.Text.RegularExpressions; public class EntryPoint {
static void Main( string[] args ) { // Create regex to search for IP address pattern. string pattern = @"(?<part1>[01]?\d\d?|2[0-4]\d|25[0-5])\." + @"(?<part2>[01]?\d\d?|2[0-4]\d|25[0-5])\." + @"(?<part3>[01]?\d\d?|2[0-4]\d|25[0-5])\." + @"(?<part4>[01]?\d\d?|2[0-4]\d|25[0-5])"; Regex regex = new Regex( pattern ); Match match = regex.Match( "192.168.169.1" ); MatchEvaluator eval = new MatchEvaluator(EntryPoint.IPReverse ); Console.WriteLine( regex.Replace("192.168.169.1", eval) ); } static string IPReverse( Match match ) { Console.WriteLine( match.Groups["part4"] + "." ); Console.WriteLine( match.Groups["part3"] + "." ); Console.WriteLine( match.Groups["part2"] + "." ); Console.WriteLine( match.Groups["part1"] ); return ""; }
}
</source>
Match Groups
<source lang="csharp"> using System; using System.Text.RegularExpressions; public class EntryPoint {
static void Main( string[] args ) { // Create regex to search for IP address pattern. string pattern = @"(?<part1>[01]?\d\d?|2[0-4]\d|25[0-5])\." + @"(?<part2>[01]?\d\d?|2[0-4]\d|25[0-5])\." + @"(?<part3>[01]?\d\d?|2[0-4]\d|25[0-5])\." + @"(?<part4>[01]?\d\d?|2[0-4]\d|25[0-5])"; Regex regex = new Regex( pattern ); Match match = regex.Match( "192.168.169.1" ); while( match.Success ) { Console.WriteLine( "IP Address found at {0} with " + "value of {1}", match.Index, match.Value ); Console.WriteLine( "Groups are:" ); Console.WriteLine( "\tPart 1: {0}", match.Groups["part1"] ); Console.WriteLine( "\tPart 2: {0}", match.Groups["part2"] ); Console.WriteLine( "\tPart 3: {0}", match.Groups["part3"] ); Console.WriteLine( "\tPart 4: {0}", match.Groups["part4"] ); match = match.NextMatch(); } }
}
</source>
Match IP address pattern and print out the index
<source lang="csharp"> using System; using System.Text.RegularExpressions; public class EntryPoint {
static void Main( string[] args ) { // Create regex to search for IP address pattern. string pattern = @"([01]?\d\d?|2[0-4]\d|25[0-5])\." + @"([01]?\d\d?|2[0-4]\d|25[0-5])\." + @"([01]?\d\d?|2[0-4]\d|25[0-5])\." + @"([01]?\d\d?|2[0-4]\d|25[0-5])"; Regex regex = new Regex( pattern ); Match match = regex.Match( "192.169.168.1" ); while( match.Success ) { Console.WriteLine( "IP Address found at {0} with " + "value of {1}", match.Index, match.Value ); match = match.NextMatch(); } }
}
</source>
Use regular to search an IP address
<source lang="csharp"> using System; using System.Text.RegularExpressions; public class EntryPoint {
static void Main( string[] args ) { // Create regex to search for IP address pattern. string pattern = @"\d\d?\d?\.\d\d?\d?\.\d\d?\d?\.\d\d?\d?"; Regex regex = new Regex( pattern ); Match match = regex.Match( "192.168.169.1" ); while( match.Success ) { Console.WriteLine( "IP Address found at {0} with " + "value of {1}", match.Index, match.Value ); match = match.NextMatch(); } }
}
</source>