CodeSamplez.com

Programming, Web development, Cloud Technologies

  • Facebook
  • Google+
  • RSS
  • Twitter
  • Home
  • Featured
    • C# Tutorials
      • LinQ Tutorials
      • Facebook C# API Tutorials
    • PHP Tutorials
      • CodeIgniter Tutorials
    • Amazon AWS Tutorials
  • Categories
    • Programming
    • Development
    • Database
    • Web Server
    • Source Control
    • Management
    • Project
  • About
  • Write
  • Contact
Home Programming Using Regular Expression In C#.NET

Using Regular Expression In C#.NET

Rana Ahsan January 5, 2011 1 Comment


 Using Regular Expression In C#.NET    

Today, in this tutorial, we are going to learn about c# regular expression programming and its practical implementation. Almost all programming languages have support for regular expression and thus has a regular expression engine which parse the expressions, compute necessarily and return expected result. .NET manages this regular expression engine globally and thus works exactly same way for all languages inside .NET framework. If you using some different language of .NET(j#,VB etc) other than c#, you can implement this similar way , just by changing the syntax to that language.

Don’t you know anything about Regular Expression at all? So, it is best to get some basic idea of regular expression before you start implementing it in C#. Go ahead and learn some regular expression basics and know what the symbols stands for.

RegularExpressions Namespace in .NET Framework:

First, we will need to import the System.Text.RegularExpressions namespace so that the regular expression classes are available. There are several classes for regular expression operation purpose like Group,Match,Capture and their corresponding Collection classes, one delegate ‘MatchEvaluator’, Regex(Main class to be used), Enum ‘RegexOptions’ and RegexCompilationInfo class, used for using Regular expression on stand alone assembly.

.NET Regular Expression Classes

Instead of Regex,Match and RegexOptions classes, other classes are for a little advance usage. So, I am not going to cover them in this article. Match class store a single match result and MatchCollection stores a list of match results. RegexOptions is an enum which has various options to pass as parameter while calling match function like Ingore case(accepts both lower and higher case letters), RightToLeft(Matches On reverse order then default left to right matching) and so on.

Using C# Regular Expression ‘Regex’ class:

As I already mentioned, Regex is the main class to do the real match operation. You can use this class either by instantiation an object, or by calling its static method. If you have such situation where same regular expression will going to be needed several times in a single execution, then its better to use instantiated object and check that against different values. Here are sample code for such usage:

            string[] email_lists = { "email1@domain1.com", "email2@domain1.com", "email1@domain2.com" };

            Regex regx = new Regex(@"[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6}$", RegexOptions.IgnoreCase);

            foreach (string email in email_lists)
            {
                Match match = regx.Match(email);
                Console.WriteLine(match.Success);
            }

The above example will validate a list of email addresses whether they are in correct email format and will print true/false depending on the match result. Note that, it matches every single string separately and returns a ‘Match’ class instance, which has a ‘Success’ property that indicates whether matches are successful or failed.

Alternative Static Match function:

If we use the static ‘Match’ method of Regex class, we will have to feed two parameter at least(+1, optional RegexOptions). The following code sample will illustrate this usage:

            string[] email_lists = { "email1domain1.com", "email2@domain1.com", "email1@domain2.com" };          
            string pattern = @"[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6}$";
            foreach (string email in email_lists)
            {
                Match match = Regex.Match(email, pattern ,RegexOptions.IgnoreCase);
                Console.WriteLine(match.Success);
            }

The above code snippet will work similarly as the previous one, just we have change the way of usage. But final result is same.

Retrieve all matched results from A Text:

OK, so as we know can match to a specific data, lets move one step forward to retrieves a list of data items which matches a specific regular expression. The following code snippet will do this work and retrieve all data matching simple time format hh:mm:ss .

 Regex dataPointRegex = new Regex(@"\d[\d+.:]+\d", RegexOptions.IgnorePatternWhitespace);
 String longText = "03:08:12asdasd06:47:12asdasd03:08:12asdasd06:47:42asdasdasdasd03:08:12ghjklfghdgodjg06:48:12asdada03:08:12asdasdasd06:48:42asdasdasdasdas";           
 List<String> segments = new List<String>();
 MatchCollection matches;
 matches = dataPointRegex.Matches(longText );

 if (matches.Count > 0)
 {
      foreach (Match match in matches)
      {
        if (match.Success)
        {
          foreach (Group result in match.Groups)
          {
             segments.Add(result.Value);
          }
        }
      }
 }

Note that, this example usage ‘Matches’ function instead of ‘Match’ to catch multiple matches. And each match object has its own associated ‘Groups’ collection that contains the results.

References:

Microsoft Product, VisualStudio.NET has a very rich online msdn documentation for full references on .NET framework. To know more details on regular expression tutorial in c#, you can go through the .NET Regular Expressions On MSDN Online. There you will find some handy examples also. Also, let me know if you having trouble on any specific portion which you haven’t found yet. Happy coding 🙂

Related

Filed Under: Programming Tagged With: c#, regular expression

About Rana Ahsan

Rana is a passionate software engineer/Technology Enthusiast.
Github: ranacseruet

Comments

  1. Kuldeep Bansal says

    November 16, 2011 at 9:26 am

    Really this is a good article, which helps a lot for beginners as me as well as developer. Check this link….
    http://mindstick.com/Blog/100/Regular%20Expressions%20in%20C

    also helpful.

    Reply

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Email Subscription

Never miss any programming tutorial again.

Popular Tutorials

  • How To Work With JSON In Node.js / JavaScript
  • PHP HTML5 Video Streaming Tutorial
  • How To Work With C# Serial Port Communication
  • LinQ Query With Like Operator
  • Facebook C# API Tutorials
  • LinQ To SQL Database Update Operations In C#
  • Using Supervisord Web Interface And Plugin
  • Tutorial On Uploading File With CodeIgniter Framework / PHP
  • Utilizing Config File In C#.NET Application
  • Using GIT Plugin For Netbeans IDE

Recent Tutorials

  • Building Auth With JWT – Part 1
  • Document Your REST API Like A Pro
  • Understanding Golang Error Handling
  • Web Application Case Studies You Must Read
  • Getting Started With Golang Unit Testing
  • Getting Started With Big Data Analytics Pipeline
  • NodeJS Tips And Tricks For Beginners
  • Apple Push Notification Backend In NodeJS
  • Web Based Universal Language Translator, Voice/Text Messaging App
  • How To Dockerize A Multi-Container App From Scratch

Recent Comments

  • S. Chalisque on PHP HTML5 Video Streaming Tutorial
  • Armorik on Generate HTTP Requests using c#
  • iswaps on PHP HTML5 Video Streaming Tutorial
  • TAKONDWA on PHP HTML5 Video Streaming Tutorial
  • rorenzo on PHP HTML5 Video Streaming Tutorial

Archives

Resources

  • CodeSamplez.com Demo

Tags

.net apache api audio aws c# cache cloud server codeigniter deployment doctrine facebook git github golang htaccess html5 http image java javascript linq mysql nodejs oop performance php phpmyadmin plugin process python regular expression scalability server smarty ssh tfs thread tips ubuntu unit-test utility web application wordpress wpf

Copyright © 2010 - 2022 · CodeSamplez.com ·

Copyright © 2022 · Streamline Pro Theme on Genesis Framework · WordPress · Log in