Search

Categories

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Send mail to the author(s) E-mail

# Tuesday, 11 November 2014
( regex )

public static void Main(){ var security = "123-12-1234"; var pattern = @"^\d{3}-\d{2}-\d{4}$"; var result = Regex.Match(security, pattern); Console.WriteLine(result); }

d – Digit

public static void Main(){ var security = "ABC 123"; //var security = "123 ABC"; var pattern = @"(^\d{3} [A-Z]{3}$)|(^[A-Z]{3} \d{3}$)"; var result = Regex.Match(security, pattern); Console.WriteLine(result); }

Or..

public static void Main() { #if DEBUG Console.WriteLine("Mode=Debug"); #else Console.WriteLine("Mode=Release"); #endif #if TRACE Console.WriteLine("Mode=On"); #else // never called Console.WriteLine("Mode=Off"); #endif }

Trace On is called in Debug and Release modes.

Crypto

public static void Main() { string data = "asdf"; byte[] userData = GetBytes(data); using (SHA256 shaM = new SHA256Managed()) { var result = shaM.ComputeHash(userData); } } static byte[] GetBytes(string str) { byte[] bytes = new byte[str.Length * sizeof(char)]; System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length); return bytes; }

Strong Names

A strong name consists of the assembly's identity — its simple text name, version number, and culture information (if provided) — plus a public key and a digital signature.

You can use strong naming to ensure that when you load a DLL you get exactly the DLL you were expecting and not some other DLL that happens to have the same name.

http://stackoverflow.com/questions/2982780/what-is-strong-name-in-net

| | # 
# Saturday, 26 May 2012
( JonSkeet | regex | TekPub )
[Test]
public void RegexTest1()
{
    // When worth using regex? Or use more primitive
    // If you have a complex pattern.. have to use many primitive operations

    Regex pattern = new Regex(" ");
    string text = "Jon Skeet";
    string[] words = pattern.Split(text);
    Assert.AreEqual("Jon", words[0]);
    Assert.AreEqual("Skeet", words[1]);
}

[Test]
public void RegexTest2()
{
    Regex pattern = new Regex(@"\d");//digit..verbatim string literal
    string text = "Jon0Skeet1Rob2Conery";
    string[] words = pattern.Split(text);
    Assert.AreEqual("Jon", words[0]);
    Assert.AreEqual("Skeet", words[1]);
    Assert.AreEqual("Rob", words[2]);
    Assert.AreEqual("Conery", words[3]);
}

[Test]
// Using regex to parse a log file
// ie using regex to pattern match
public void RegexTest3()
{
    Regex pattern = new Regex(@"\d");//digit..verbatim string literal
    string text = "Jon0Skeet1Rob2Conery";
    string[] words = pattern.Split(text);
    Assert.AreEqual("Jon", words[0]);
    Assert.AreEqual("Skeet", words[1]);
    Assert.AreEqual("Rob", words[2]);
    Assert.AreEqual("Conery", words[3]);
}

[Test]
public void PatternMatchLogFileParser()
{
    string sampleLine = "WARNING 05/10/2011 13:15:15.000 ---FooBar--- The foo has been barred";

    //john finds this hard!  MSDN regex is good
    Regex pattern = new Regex(@"(?<level>\S+) " + //brackets is grouping.. and name.  \S is non space chars   + means 1 or more  space
                              @"(?<timestamp>\d{2}/\d{2}/\d{4} \d{2}:\d{2}:\d{2}\.\d{3}) " + // \d digit exactly 2 of them then forwardslash then 2 digits forwardslash then 4 digits escape the dot which is anything
                              @"---(?<category>[^-]+)--- " + //not a hypen in category.. carot is anything except
                              @"(?<message>.*)");//dot is anything, star is 0 or more times

    Match match = pattern.Match(sampleLine);//Matches tries to match the pattern multiple times
    if (match.Success)
    {
        string level = match.Groups["level"].Value;
        Console.WriteLine(level);
        string timestamp = match.Groups["timestamp"].Value;
        DateTime dateTime = DateTime.ParseExact(timestamp, "dd/MM/yyyy HH:mm:ss.fff", CultureInfo.InvariantCulture);
        DateTime date = dateTime.Date;
        Console.WriteLine(date);
        Console.WriteLine(match.Groups["category"]);
        Console.WriteLine(match.Groups["message"]);
    }
}

[Test]
public void TakeOutAllTheVowels()
{
    string sampleLine = "Hello, world";
    Regex pattern = new Regex("[aeiou]");
    string noVowels = pattern.Replace(sampleLine, "?");
    Assert.AreEqual("H?ll?, w?rld", noVowels);
}

[Test]
public void TakeOutAllTheVowels2()
{
    string sampleLine = "Hello, world";
    //Regex pattern = new Regex("[aeiou]"); //simpler here!
    //string noVowels = pattern.Replace(sampleLine, "?");
    string noVowelHardWay = sampleLine.Replace('a', '?') //character literal
                                        .Replace("e", "?") //method chaining
                                        .Replace("i", "?")
                                        .Replace("o", "?")
                                        .Replace("u", "?");
    Assert.AreEqual("H?ll?, w?rld", noVowels);
}
| | #