using System;
class MathOpsApp
{
static void Main(string[] args)
{
Random rand = new Random();
int a, b, c;
a = rand.Next() % 100; // Limit max to 99.
b = rand.Next() % 100; // Limit max to 99.
Console.WriteLine("a={0} b={1}", a, b);
c = a * b;
Console.WriteLine("a * b = {0}", c);
c = a / b;
Console.WriteLine("a / b = {0}", c);
c = a + b;
Console.WriteLine("a + b = {0}", c);
c = a - b;
Console.WriteLine("a - b = {0}", c);
c = a % b;
Console.WriteLine("a % b = {0}", c);
}
}
Month: February 2011
MatchEvaluator: Entry Point IP Reverse
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]?dd?|2[0-4]d|25[0-5])." +
@"(?<part2>[01]?dd?|2[0-4]d|25[0-5])." +
@"(?<part3>[01]?dd?|2[0-4]d|25[0-5])." +
@"(?<part4>[01]?dd?|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 "";
}
}
Define multiline patterns
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]?dd?
# OR
|2[0-4]d
# OR
|25[0-5])
.
# REPEAT
([01]?dd?|2[0-4]d|25[0-5]).
# REPEAT
([01]?dd?|2[0-4]d|25[0-5]).
# REPEAT
([01]?dd?|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();
}
}
}
Match Groups
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]?dd?|2[0-4]d|25[0-5])." +
@"(?<part2>[01]?dd?|2[0-4]d|25[0-5])." +
@"(?<part3>[01]?dd?|2[0-4]d|25[0-5])." +
@"(?<part4>[01]?dd?|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( " Part 1: {0}",
match.Groups["part1"] );
Console.WriteLine( " Part 2: {0}",
match.Groups["part2"] );
Console.WriteLine( " Part 3: {0}",
match.Groups["part3"] );
Console.WriteLine( " Part 4: {0}",
match.Groups["part4"] );
match = match.NextMatch();
}
}
}
Is Match successful
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]?dd?|2[0-4]d|25[0-5])." +
@"([01]?dd?|2[0-4]d|25[0-5])." +
@"([01]?dd?|2[0-4]d|25[0-5])." +
@"([01]?dd?|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( " {0} at {1}",
g.Value,
g.Index );
}
match = match.NextMatch();
}
}
}
Match IP address pattern and print out the index
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]?dd?|2[0-4]d|25[0-5])." +
@"([01]?dd?|2[0-4]d|25[0-5])." +
@"([01]?dd?|2[0-4]d|25[0-5])." +
@"([01]?dd?|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();
}
}
}
Use regular to search an IP address
using System;
using System.Text.RegularExpressions;
public class EntryPoint
{
static void Main( string[] args ) {
// Create regex to search for IP address pattern.
string pattern = @"dd?d?.dd?d?.dd?d?.dd?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();
}
}
}

