C# Code Snippets  C# Code Snippets
 C# Code Snippets  C# Code Snippets
 C# Code Snippets  C# Code Snippets
 C# Code Snippets  C# Code Snippets
Showing posts with label Regular Expressions. Show all posts
Showing posts with label Regular Expressions. Show all posts

Wednesday, December 19, 2007

Expresso - a Great Regular Expression Tool

Hi All

This one is for all you guys who are tired of pressing F5 over and over in Visual studio just to see if you got the Regular expression right, I know got tired of it and that's how i found this tool.

Introducing Expresso the "Visual Studio of Regular Expression":

You can Download it free of charge from here:

http://www.ultrapico.com/ExpressoDownload.htm

After you install it and run check the help section, there's a very useful tutorial there. basically your screen is split into four: Top left corner is where you type in the pattern you are looking for, top right is the analyzer which compiles the pattern on the left as you type it, bottom left is the actual text you are parsing and on the bottom right are the matches your pattern turned out. In order to check for matches you just have to click "Run Mach" which is located just above the top left window. you can try and use my other example for regular expressions (http://dev102.blogspot.com/2007/12/regular-expressions.html) on Expresso to check how it works.

The most important thing is that after you are satisfied with the results click on Tools->View Code and Viola, on the bottom right you get the actual C# code for your application

untitled

Comment if you have any questions

Enjoy

Amit

AddThis Social Bookmark Button

Thursday, December 13, 2007

How to use Regular Expressions

Hi

Ever needed to parse a web page and get all the Links in it (href's)? the easy way is to use this regular expression to get the href:

Regex r = new Regex("href.*)";

for those of you who don't know this means get me something that starts with -href- and then: whatever... that's what the -.*- is for. The problem is that now we have to work on the results in order to get the actual link.

Extra work? I don't think so...

We want to use groups, so the regular expression will look like this:

"href.*?"(?<HREF>.*?)"

Or in code: (we need to add \ for some escape characters)

Regex MyRegex = new Regex("href.*?\"(?<href>.*?)\"",RegexOptions.Multiline);


The RegexOptions.Multiline means that we can provide a multiline string as the input of the Regular expression


lets break it down:


href.*?"(?<HREF>.*?)"



The beginning is the same -href.*- get everything that starts with href now comes the twist.



the -?"- means stop on the first " you find, if we drop the -?- he will stop on the last -"- he finds (greedy!!!). Now comes the definition of the group: -(?<HREF>.*?) the syntax for defining a group is :



(?<GroupName><Rule>)



What comes after the Group name is the regular expression for the group, in our case the end looks like this:



.*?)"



which means get everything until the first " you see.



that way we will get the "clean" URL inside the HREF group!



To use the groups use this code:



public static void GetMatches(string s)
{
Regex MyRegex = new Regex("href.*?\"(?<href>.*?)\"", RegexOptions.Multiline);
MatchCollection mc1 = MyRegex.Matches(s);
Console.WriteLine(MyRegex.ToString());
foreach (Match m1 in mc1)
{
Console.WriteLine("URL: {0}", m1.Groups["href"].Value);
}
}

Credit to Shahar A.

Have fun!!

Amit

AddThis Social Bookmark Button