using System;
using System.Text.RegularExpressions;
using System.Reflection;
class Class1
{
[STAThread]
static void Main(string[] args)
{
Regex cardExp = new Regex( @"(\d{4})-(\d{4})-(\d{4})-(\d{4})" );
string safeOutputExp = "$1-xxxx-xxxx-$4";
string cardNum;
cardNum = "asdf-asdf-asdf-asdf";
Console.WriteLine( cardExp.IsMatch( cardNum ) );
Console.WriteLine( "Secure Output Result = {0}", cardExp.Replace( cardNum, safeOutputExp ));
TestManualGrouping();
CompileRegex( cardExp.ToString(), @"regexlib" );
}
static void CompileRegex( string exp, string assemblyName )
{
RegexCompilationInfo compInfo = new RegexCompilationInfo( exp, 0, "CreditCardExp", "", true );
AssemblyName assembly = new AssemblyName();
assembly.Name = assemblyName;
RegexCompilationInfo[] rciArray = { compInfo };
Regex.CompileToAssembly( rciArray, assembly );
}
static void TestManualGrouping()
{
Regex cardExp = new Regex( @"\d{4}" );
string cardNum;
cardNum = "asdf-asdf-asdf-asdf";
if( cardExp.Matches( cardNum ).Count < 4 )
{
Console.WriteLine( "Invalid card number" );
return;
}
foreach( Match field in cardExp.Matches( cardNum ))
{
if( field.Success == false )
{
Console.WriteLine( "Invalid card number" );
return;
}
if( field.Index == 5 || field.Index == 10 )
{
Console.WriteLine("-xxxx-");
}
else
{
Console.WriteLine(field.Value);
}
}
}
}
|