Thursday, May 26, 2011

C# Regular Expressions

What is Regular Expression
Regular expressions allow for easy parsing and matching of strings to a specific pattern. Using the objects available in the RegularExpressions namespace, you can compare a string against a given pattern, replace a string pattern with another string, or retrieve only portions of a formatted string. In this example, we will construct a pattern to validate a e-mail address.

Using regular expressions to match a pattern


  1. Start Visual C#.

  2. Create a new Visual C# Console Application.

  3. Specify the using keyword on the Text.RegularExpressions namespace so that you will not be required to qualify declarations in those namespaces later in your code. The using statement must be used prior to any other declarations:
    using System.Text.RegularExpressions;

  4. Define a new regular expression that will use a pattern match to validate an e-mail address. The following regular expression is structured to accomplish three things: *Capture the substring before the @ symbol and put that into the "user" group.
    *Capture the substring after the @ symbol and put that into the "host" group.
    *Make sure that the first half of the string does not have an @ symbol.

  5. Regex emailregex = new Regex("(?[^@]+)@(?.+)");


  6. Define a new string containing a valid e-mail address. This provides a default value if the method's command-line argument is empty:
  7. String s = "johndoe@tempuri.org";


  8. Check to see if there are any command-line parameters; if there are, retrieve the first parameter and assign it to the variable "s".
  9. if ( args.Length > 0 ) {
    s = args[0];
    }


  10. Use the Match method to pass in the e-mail address variable and return a new Match object. The Match object will return regardless of whether any matches were found in the source string.
  11. Match m = emailregex.Match(s);


  12. By examining the Success property, we can decide whether to continue processing the Match object or to print an error message. If successful, display the "user" and "host" named groups within the Groups collection of the Match object.
    if ( m.Success ) {
    Console.WriteLine("User: " + m.Groups["user"].Value);
    Console.WriteLine("Host: " + m.Groups["host"].Value);
    } else {
    Console.WriteLine(s + " is not a valid email address");
    }
    Console.WriteLine();

  13. To keep the console window open after running the application, add the following lines of code:
    System.Console.WriteLine("Press Enter to Continue...");
    System.Console.ReadLine();

  14. Build your project.

To run the application in the development environment using the default e-mail address specified in the code, press F5 or select Start from the Debug menu. To start the application with a command-line argument, there are three options:
On the Project menu, click Properties, and then click Debug. In the Start Options section in the right pane, specify the e-mail address that you want to test. Press F5, or click Start on the Debug menu to run the application.


Available Regular Expression Operators


More Info:
The book Mastering Regular Expressions not only explains everything you want to know and don't want to know about regular expressions, including the regex features that are unique to .NET. It has an excellent chapter on .NET's System.Text.RegularExpressions namespace, explaining the various Regex classes far better than Microsoft's documentatio, with plenty of example VB.NET example code and some C# code showing more advanced techniques.


    Monday, August 31, 2009

    Batch files for changing IP Address and Domains

    The following post provide information about how to create batch files for changing IP Addresses and Domains easily. Just copy the contents marked between ****** into a text file and give the file extension as .bat


    CHANGING IP ADDRESS

    We use the netsh command to change the IP address. The following batch files help in changing the IP Address from dynamic to static and vice versa:

    Provide Static IP
    ********************************************************************************
    @ECHO OFF

    set varip=192.68.10.12
    set varsm=255.255.255.248
    set vargw=68.23.41.1
    set vardns1=68.23.41.1
    set vardns2=68.23.41.2

    REM ***** You don’t need to change anything below this line! ******

    ECHO Setting IP Address and Subnet Mask
    netsh int ip set address name = "Local Area Connection" source = static addr = %varip% mask = %varsm%

    ECHO Setting Gateway
    netsh int ip set address name = "Local Area Connection" gateway = %vargw% gwmetric = 1

    ECHO Setting Primary DNS
    netsh int ip set dns name = "Local Area Connection" source = static addr = %vardns1%

    ECHO Setting Secondary DNS
    netsh int ip add dns name = "Local Area Connection" addr = %vardns2%

    ECHO Here are the new settings for %computername%:
    netsh int ip show config

    pause
    ********************************************************************************

    Remove Static IP and change to Dynamic
    ********************************************************************************
    @ECHO OFF

    ECHO Setting IP Address and Subnet Mask to Dynamic
    netsh int ip set address name = "Local Area Connection" source=dhcp
    ECHO Setting DNS Server Address to Dynamic
    netsh int ip set dns name = "Local Area Connection" source = dhcp

    ECHO Renewing IP Address
    ipconfig /renew

    pause
    ********************************************************************************

    CHANING DOMAINS

    To change the domain, we need to use the netdom.exe, which will get installed along with the Windows XP Support Tools.

    To Join to a Domain
    ********************************************************************************
    netdom join %computername% /d:domainName /ud:userName /pd:password /reboot:XX in seconds after which computer reboots
    ********************************************************************************

    To Remove from Domain
    ********************************************************************************
    netdom remove %computername% /d:domainName /ud:userName /pd:password /reboot:XX
    ********************************************************************************


    Installing Netdom.exe

    Installation File Name: WindowsXP-KB838079-SupportTools-ENU
    Download Location: Download
    Command Information: See Here

    After installation of the support tools, the netdom.exe will be available in C:\Program Files\Support Tools. Copy this file into the location where you will be running the batch file.