Hi,
Below are the links to all the posts which i have created for topic regular expessions. All the topics are explained in C#.net Examples.
Learning regular expression step by step tutorials: Step 1
Learning regular expression step by step tutorials: Step 2
Learning regular expression step by step tutorials: Step 3
Learning regular expression step by step tutorials: Step 4
Learning regular expression step by step tutorials: Step 5
How to match using Backreferences in regular expressions?
Extract matched data using regular expressions C# example
How to use back references to match the patterns?
How to extract matched data from a source and replace substrings using regular expressions
Hope you find these resourceful.
Regards
Sameer Shaik
Tags: Microsoft MCTS exam, Learn, Tutorials, Free C#, Free Videos, Microsoft Dot Net Jobs,Microsoft Dot Net Jobs Interview, Free Training and Preparation, Jobs MCTS specialization, visual studio.net, study guide, sample questions, Exam Prep, Exam Practice Test
Free Online Training VIDEOS,SAMPLES,TUTORIALS ON MCTS EXAM C#.NET(MS VISUAL STUDIO) Thanks for your support. The blog is getting bigger and better day by day. please check out : www.blog.sameershaik.com for further interesting articles on other microsoft certification examinations
Search this blog
You can search for all the topics in our blog using our google custom search now.
Search the blog
Loading
Showing posts with label Topic Regular Expressions. Show all posts
Showing posts with label Topic Regular Expressions. Show all posts
Friday, September 3, 2010
Topic : Regular Expressions
How to match using Backreferences in regular expressions?
This video tutorial will provide you with litlle insight on how to use back references in regular expressions.
If you are new to regular expressions you need to check the previous tutorial posts which will teach you some basics of regular expressions.
Regards
Sameer Shaik
Tags: Microsoft MCTS exam, Learn, Tutorials, Free C#, Free Videos, Microsoft Dot Net Jobs,Microsoft Dot Net Jobs Interview, Free Training and Preparation, Jobs MCTS specialization, visual studio.net, study guide, sample questions, Exam Prep, Exam Practice Test
If you are new to regular expressions you need to check the previous tutorial posts which will teach you some basics of regular expressions.
Regards
Sameer Shaik
Tags: Microsoft MCTS exam, Learn, Tutorials, Free C#, Free Videos, Microsoft Dot Net Jobs,Microsoft Dot Net Jobs Interview, Free Training and Preparation, Jobs MCTS specialization, visual studio.net, study guide, sample questions, Exam Prep, Exam Practice Test
How to extract matched data from a source and replace substrings using regular expressions
In this tutorial we will detect a specific pattern from given source file and edit the source file automatically with our custom format substring pattern
Video Part - 1
Video Part - 2
Regards
Sameer Shaik
Tags: Microsoft MCTS exam, Learn, Tutorials, Free C#, Free Videos, Microsoft Dot Net Jobs,Microsoft Dot Net Jobs Interview, Free Training and Preparation, Jobs MCTS specialization, visual studio.net, study guide, sample questions, Exam Prep, Exam Practice Test
Video Part - 1
Video Part - 2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.IO;
using System.Threading;
using System.Security.Permissions;
using System.Text.RegularExpressions;
namespace Example1
{
class Program
{
static void Main(string[] args)
{
string SampleText = CreateString();
DisplayMatches(SampleText);
Console.WriteLine("We will now replace the dates with our custom format to the file");
string ChangedText = ReplaceString(SampleText);
WriteToFile(ChangedText);
Console.ReadKey();
}
static string CreateString()
{
FileStream fs = new FileStream(@"C:\My stuff\Work Stuff\Blog\Test\SampleText.txt", FileMode.Open);
StreamReader sr = new StreamReader(fs);
string s = sr.ReadToEnd();
sr.Close();
fs.Close();
return s;
}
static void DisplayMatches(string s)
{
Regex r;
Match m;
for (m = r.Match(s); m.Success; m = m.NextMatch())
{
Console.WriteLine("Found a date in the source"
+ " "+ m.Result("${day}/${month}/${year}"));
}
}
static string ReplaceString(string s)
{
}
static void WriteToFile(string s)
{
FileStream fs = new FileStream(@"C:\My stuff\Work Stuff\Blog\Test\SampleText.txt", FileMode.Open,FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
sw.Write(s);
sw.Close();
fs.Close();
}
}
}
Regards
Sameer Shaik
Tags: Microsoft MCTS exam, Learn, Tutorials, Free C#, Free Videos, Microsoft Dot Net Jobs,Microsoft Dot Net Jobs Interview, Free Training and Preparation, Jobs MCTS specialization, visual studio.net, study guide, sample questions, Exam Prep, Exam Practice Test
Thursday, September 2, 2010
How to use back references to match the patterns?
Hi,
I have provided a sample example of how to retrieve matched data from a text file using backreferences. This tutorial will help us in understanding how to create a regular expression for matching the repeating patterns from a source.I will explain in the video tutorial about how to match repeating letters and how to match repeating words from a given source. We will also see how to construct a regular expression for the above scenarios.After we do that we will see how to extract matched data from the source and use it for our purpose.
Video tutorial part - 1
Video Tutorial Part - 2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.IO;
using System.Threading;
using System.Security.Permissions;
using System.Text.RegularExpressions;
namespace Example1
{
class Program
{
static void Main(string[] args)
{
string SampleText = CreateString();
DisplayMatches(SampleText);
Console.ReadKey();
}
static string CreateString()
{
FileStream fs = new FileStream(@"C:\My stuff\Work Stuff\Blog\Test\SampleText.txt", FileMode.Open);
StreamReader sr = new StreamReader(fs);
string s = sr.ReadToEnd();
sr.Close();
fs.Close();
return s;
}
static void DisplayMatches(string s)
{
Regex r;
Match m;
for (m = r.Match(s); m.Success; m = m.NextMatch())
{
Console.WriteLine("Found repeating words at "
+ m.Groups[1].Index + " And the macth is " + m.Groups[1].Value);
}
}
}
}
Regards
Sameer Shaik
Tags: Microsoft MCTS exam, Learn, Tutorials, Free C#, Free Videos, Microsoft Dot Net Jobs,Microsoft Dot Net Jobs Interview, Free Training and Preparation, Jobs MCTS specialization, visual studio.net, study guide, sample questions, Exam Prep, Exam Practice Test
I have provided a sample example of how to retrieve matched data from a text file using backreferences. This tutorial will help us in understanding how to create a regular expression for matching the repeating patterns from a source.I will explain in the video tutorial about how to match repeating letters and how to match repeating words from a given source. We will also see how to construct a regular expression for the above scenarios.After we do that we will see how to extract matched data from the source and use it for our purpose.
Video tutorial part - 1
Video Tutorial Part - 2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.IO;
using System.Threading;
using System.Security.Permissions;
using System.Text.RegularExpressions;
namespace Example1
{
class Program
{
static void Main(string[] args)
{
string SampleText = CreateString();
DisplayMatches(SampleText);
Console.ReadKey();
}
static string CreateString()
{
FileStream fs = new FileStream(@"C:\My stuff\Work Stuff\Blog\Test\SampleText.txt", FileMode.Open);
StreamReader sr = new StreamReader(fs);
string s = sr.ReadToEnd();
sr.Close();
fs.Close();
return s;
}
static void DisplayMatches(string s)
{
Regex r;
Match m;
for (m = r.Match(s); m.Success; m = m.NextMatch())
{
Console.WriteLine("Found repeating words at "
+ m.Groups[1].Index + " And the macth is " + m.Groups[1].Value);
}
}
}
}
Regards
Sameer Shaik
Tags: Microsoft MCTS exam, Learn, Tutorials, Free C#, Free Videos, Microsoft Dot Net Jobs,Microsoft Dot Net Jobs Interview, Free Training and Preparation, Jobs MCTS specialization, visual studio.net, study guide, sample questions, Exam Prep, Exam Practice Test
Tuesday, August 24, 2010
Extract matched data using regular expressions C# example
This example will help you understand how to extract matched data using regular expressions and display it on the console.
Watch this tutorial first on using BackReferences:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
namespace Regex1
{
class Program
{
static void Main(string[] args)
{
string htmlfile = CreateString();
DisplayMatches(htmlfile);
Console.ReadKey();
}
static string CreateString()
{
FileStream fs = new FileStream(@"C:\Users\Ship\Documents\Visual Studio 2008\Projects\Regex1\Regex1\Sample.aspx", FileMode.Open);
StreamReader sr = new StreamReader(fs);
string s = sr.ReadToEnd();
return s;
}
static void DisplayMatches(string s)
{
Regex r;
Match m;
r = new Regex(@"href\s*=\s*(?:""(?<1>[^""]*)""|(?<1>\S+))", RegexOptions.IgnoreCase|RegexOptions.Compiled);
for (m = r.Match(s); m.Success; m = m.NextMatch())
{
Console.WriteLine("Found href " + m.Groups[1] + " at "
+ m.Groups[1].Index);
}
}
}
}
Video tutorial for this post will be uploaded soon.
Regards
Sameer Shaik
Tags: Microsoft MCTS exam, Learn, Tutorials, Free C#, Free Videos, Microsoft Dot Net Jobs,Microsoft Dot Net Jobs Interview, Free Training and Preparation, Jobs MCTS specialization, visual studio.net, study guide, sample questions, Exam Prep, Exam Practice Test
Watch this tutorial first on using BackReferences:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
namespace Regex1
{
class Program
{
static void Main(string[] args)
{
string htmlfile = CreateString();
DisplayMatches(htmlfile);
Console.ReadKey();
}
static string CreateString()
{
FileStream fs = new FileStream(@"C:\Users\Ship\Documents\Visual Studio 2008\Projects\Regex1\Regex1\Sample.aspx", FileMode.Open);
StreamReader sr = new StreamReader(fs);
string s = sr.ReadToEnd();
return s;
}
static void DisplayMatches(string s)
{
Regex r;
Match m;
r = new Regex(@"href\s*=\s*(?:""(?<1>[^""]*)""|(?<1>\S+))", RegexOptions.IgnoreCase|RegexOptions.Compiled);
for (m = r.Match(s); m.Success; m = m.NextMatch())
{
Console.WriteLine("Found href " + m.Groups[1] + " at "
+ m.Groups[1].Index);
}
}
}
}
Video tutorial for this post will be uploaded soon.
Regards
Sameer Shaik
Tags: Microsoft MCTS exam, Learn, Tutorials, Free C#, Free Videos, Microsoft Dot Net Jobs,Microsoft Dot Net Jobs Interview, Free Training and Preparation, Jobs MCTS specialization, visual studio.net, study guide, sample questions, Exam Prep, Exam Practice Test
Sunday, July 4, 2010
Learning regular expression step by step tutorials: Step 5
Free Microsoft MCTS 70-536 Examination Training and Preparation for Jobs
| ||||||||
Tags: Microsoft MCTS exam, Learn, Tutorials, Free C#, Free Videos, Microsoft Dot Net Jobs,Microsoft Dot Net Jobs Interview, Free Training and Preparation, Jobs MCTS specialization, visual studio.net. |
Learning regular expression step by step tutorials: Step 4
Free Microsoft MCTS 70-536 Examination Training and Preparation for Jobs and Interviews | |||||||||
|
Free Microsoft MCTS 70-536 Examination Training and Preparation for Jobs and Interviews
Tags: Microsoft MCTS exam, Learn, Tutorials, Free C#, Free Videos, Microsoft Dot Net Jobs,Microsoft Dot Net Jobs Interview, Free Training and Preparation, Jobs MCTS specialization, visual studio.net. |
Wednesday, June 30, 2010
Learning regular expression step by step tutorials: Step 3
Free Microsoft MCTS 70-536 Examination Training and Preparation | |||
If you haven't watched the previous post, then check out the related topics Character Classes or Sets If you want to tell the regex engine to match with one of the several characters, then you can form Character Classes/Sets. To form a character set, we just need to place the characters in square brackets like this : [a-z], this will include all the characters from a to z or like this : [ghi], this will include characters specified in the square bracket(g,h and i) Negated Character Classes If you want to tell the regex not to match with any of the given range/set of characters, then you can use negated character classes. To form a Negated Character Class you need to add ^(A Caret sign) after opening the square bracket. Some of the examples are: [^ghi] character class will match with any character other than g,h and i. [^a-z] character class will match with any characters other than between a to z. | Related Topics
| ||
Short hand Character Classes In previous post we have seen that when you use "backslash" with literals other than special characters because "backslash" in combination with other literals creates a regex token which has a special meaning in itself. For example: when "d " is used in combination with "/", it creates a regex token "/d" which matches all digits from 0-9. The above regex token is an example of Short-hand Character Class. A short hand character class has been developed to include character classes which are used more often. Other Examples are: "\w" stands for word character which can be defined as [a-zA-Z0-9_] "\s" stands for white space character which can be defined as [ \t\r\n](space, tab, line break) Repeating Character Classes You can repeat a character class using ?,* or + operators. If you use "+" operator with a character class like this: [a-c]+ The above expressions tells the regex engine to match with any characters between a,b,c one or more times. Which means [a-c]+ matches with jukibyha since input(jukibyha) has "b" and "a". However most of the times you want to check if there are any combinations of "aaa","aba","bbc","baa","cca" etc patterns in the given input. To do that we need to use backreferences to check for repeated matched characters. If you didn't understand what i mean by repeated matched characters, then here is the following explanation: If my regular expression is [0-3] and the input is 568935413, then our regex engine finds the first match "3" and then stops even though "1" is also a valid match. So now "3" is a Matched character by the regex engine. If you want regex engine to keep searching for matched character "3" then you use backreferences to tell regex engine to save the matched characters in its memory.If you use back references, regex engine will find the first match "3", save it in the memory and it will keep searching for another "3" until end of the given input. So if you want to look for repeated matched characters, you can create a regular expression like this ([0-3])\1+.This will tell the regex engine to look for repeated matched characters for one or more time. Check for next tutorial on regular expressions Regards Sameer Shaik Free Microsoft MCTS 70-536 Examination Training and Preparation |
Wednesday, June 23, 2010
Learning regular expression step by step tutorials: Step 2
If you haven't watched the previous post, then below is the link:
Step 1 Learning regular expressions
Now we will going little further with regular expressions. In previous tutorial, we have seen that matches were done for very simple expression. In practicality we will be looking for more complex expressions to get the exact match. To provide us with these capabilities, Special Characters/Meta Characters are available.
There are 11 characters with special meanings:
1. The opening square bracket [
2. The backslash \
3. The caret ^
4. The dollar sign $
5. The period or dot .
6. The vertical bar or pipe symbol |
7. The question mark ?
8. The asterisk or star *
9. The plus sign +
10. The opening round bracket (
11. The closing round bracket ).
Since the above characters have a special meaning, we cannot find a match for "2+2" in a string "2+2 equals 4". When you use these special characters for ex: plus sign in "2+2" it will match with 22,222,2222 but not just 2+2,2,22+2 etc. '+' matches the preceding character one or more times.
If you would like special characters to be treated like literals in the previous example. That is you want "2+2" regex to match with "2+2 equals 4". Then you need to escape these special characters with "backslash(\)" like this: 2\+2 will ignore the "+" sign as literal which will now match with the given string.
Point to remember: When you use "backslash" with literals other than special characters because "backslash" in combination with other literals creates a regex token which has a special meaning in itself.
For example:
when "d " is used in combination with "/", it creates a regex token "/d" which matches all digits from 0-9.
The following example is simple demonstration of what we've learnt,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace RegexStep2
{
class Program
{
static void Main(string[] args)
{
string s = "2+2 equals 4";
string regex = @"2+2";
if (Regex.IsMatch(s, regex))
{
Console.WriteLine("Given string " + s + " matches with regular expression " + regex);
Console.ReadKey();
}
else
{
Console.WriteLine("Given string " + s + " does not match with regular expression " + regex);
Console.ReadKey();
}
}
}
}
Regards
Sameer Shaik
Step 1 Learning regular expressions
Now we will going little further with regular expressions. In previous tutorial, we have seen that matches were done for very simple expression. In practicality we will be looking for more complex expressions to get the exact match. To provide us with these capabilities, Special Characters/Meta Characters are available.
There are 11 characters with special meanings:
1. The opening square bracket [
2. The backslash \
3. The caret ^
4. The dollar sign $
5. The period or dot .
6. The vertical bar or pipe symbol |
7. The question mark ?
8. The asterisk or star *
9. The plus sign +
10. The opening round bracket (
11. The closing round bracket ).
Since the above characters have a special meaning, we cannot find a match for "2+2" in a string "2+2 equals 4". When you use these special characters for ex: plus sign in "2+2" it will match with 22,222,2222 but not just 2+2,2,22+2 etc. '+' matches the preceding character one or more times.
If you would like special characters to be treated like literals in the previous example. That is you want "2+2" regex to match with "2+2 equals 4". Then you need to escape these special characters with "backslash(\)" like this: 2\+2 will ignore the "+" sign as literal which will now match with the given string.
Point to remember: When you use "backslash" with literals other than special characters because "backslash" in combination with other literals creates a regex token which has a special meaning in itself.
For example:
when "d " is used in combination with "/", it creates a regex token "/d" which matches all digits from 0-9.
The following example is simple demonstration of what we've learnt,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace RegexStep2
{
class Program
{
static void Main(string[] args)
{
string s = "2+2 equals 4";
string regex = @"2+2";
if (Regex.IsMatch(s, regex))
{
Console.WriteLine("Given string " + s + " matches with regular expression " + regex);
Console.ReadKey();
}
else
{
Console.WriteLine("Given string " + s + " does not match with regular expression " + regex);
Console.ReadKey();
}
}
}
}
Non Printable Characters:
We can also use special characters to put non printable characters in our regular expressions.Some examples of non printable characters are as follows:
1. "\t" to match a TAB character
2. "\n" to match a Line Feed.
3. "\r" to match a Carriage Return.
etc etc
Regards
Sameer Shaik
Learning regular expression step by step tutorials: Step 1
What is a regular expression?
It is a set of characters that can be compared to a string to determine whether the string meets specified format requirements.
The most basic form of regular expression is using Literal Characters. For ex: "t" will match any first occurrence in "mcts tutorials blog", which is simply "t" in "mcts", if you notice there is a second match too, but we haven't developed regular expression to evaluate any occurrence after the first.
Create a new console application and double click on program.cs file. Add the namespace System.Text.RegularExpressions.Regex.IsMatch() is the method which is going to evaluate your regular expression i.e. It is the going to compare the given string with the regular expression. If the match is successful then it is going to return true or mismatch results in false.
Your code should look like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace RegexStep1
{
class Program
{
static void Main(string[] args)
{
string s = "Literal Characters";
string regex = "tr";
if (Regex.IsMatch(s, regex))
{
Console.WriteLine("Given string " + s + " matches with regular expression " + regex);
Console.ReadKey();
}
else
{
Console.WriteLine("Given string " + s + " does not match with regular expression " + regex);
Console.ReadKey();
}
}
}
}
Please check out the video for detailed explanation on this example.
It is a set of characters that can be compared to a string to determine whether the string meets specified format requirements.
The most basic form of regular expression is using Literal Characters. For ex: "t" will match any first occurrence in "mcts tutorials blog", which is simply "t" in "mcts", if you notice there is a second match too, but we haven't developed regular expression to evaluate any occurrence after the first.
Create a new console application and double click on program.cs file. Add the namespace System.Text.RegularExpressions.Regex.IsMatch() is the method which is going to evaluate your regular expression i.e. It is the going to compare the given string with the regular expression. If the match is successful then it is going to return true or mismatch results in false.
Your code should look like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace RegexStep1
{
class Program
{
static void Main(string[] args)
{
string s = "Literal Characters";
string regex = "tr";
if (Regex.IsMatch(s, regex))
{
Console.WriteLine("Given string " + s + " matches with regular expression " + regex);
Console.ReadKey();
}
else
{
Console.WriteLine("Given string " + s + " does not match with regular expression " + regex);
Console.ReadKey();
}
}
}
}
Please check out the video for detailed explanation on this example.
Subscribe to:
Posts (Atom)