IT SOLUTIONS
Your full service technology partner! 
-Collapse +Expand
Coder
Search Coder Group:

Advanced
-Collapse +Expand Coder Study Test
PRESTWOODCERTIFIED
-Collapse +Expand Coder Store
PRESTWOODSTORE

Prestwood eMagazine

September Edition
Subscribe now! It's Free!
Enter your email:

   ► KBRole-Based T...Coding & OOGeneral .Net...   Print This     
  From the December 2015 Issue of Prestwood eMag
 
Coder General .Net Concepts:
Regex
 
Posted 12 years ago on 10/6/2008 and updated 11/7/2008
Take Away: Console application shows some common uses of Regular Expressions (REGEX) to match and recursively replace text in strings.

KB101321

This console application shows some common uses of Regular Expressions (REGEX) to match and recursively replace text in strings.

This is very fast compared to loop compares and very powerful if you know how to use them.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Regexes
{
class Program
{

static void testMatch(string pattern, string s)
{
if (Regex.IsMatch(s,pattern) ){
Console.WriteLine(string.Format("MATCH {0,-20}",s));
} else {
Console.WriteLine(string.Format("NO MATCH {0,-20}",s));
}
}

static void Convert(string pattern, string s)
{
Console.WriteLine("{0,-20} becomes {1,-20}",s,Regex.Replace(s, pattern, string.Empty));
}

static void Main(string[] args)
{

Console.WriteLine("\n\n---\nValidate that the input is one of the 50 US States\n---");
string StatePattern = @"(:?A[KLRZ]|C[AOT]|D[CE]|FL|GA|HI|I[ADLN]|K[SY]|LA|M[ADEINOST]|N[CDEHJMVY]|O[HKR]|P[AR]|RI|S[CD]|T[NX]|UT|V[AIT]|W[AIVY])";

testMatch(StatePattern, "California");
testMatch(StatePattern, "CA");
testMatch(StatePattern, "AS");
testMatch(StatePattern, "JP");
testMatch(StatePattern, "MS");
testMatch(StatePattern, "AZ and HI");

Console.WriteLine("\n\n---\nremove HTML markup\n---");
string HTMLPattern = @"<(.|\n)*?>";
Convert(HTMLPattern, "Plain Text");

Convert(HTMLPattern, "<p>a Paragraph</p>");
Convert(HTMLPattern, "a Photo <img src=\"http://site.com/photo.jpg\" />");

Console.WriteLine("\n\n---\njpg, gif or png (or adjust to accept any kind of file.)\n---");
string ImgPattern=@"([^\s]+(?=\.(jpg|gif|png))\.\2)";
testMatch(ImgPattern, @"C:\my documents\photo.jpg");
testMatch(ImgPattern, @"Program.exe");
testMatch(ImgPattern, @"WrongKind.bmp");


Console.WriteLine("\n\n");
}
}
}

Regular Expression Program Output

Console application shows some common uses of Regular Expressions (REGEX) to match and recursively replace text in strings. This console application demonstrates some common uses.

More Info

KB Post:  Regular Expressions Cheat Sheet

Comments

1 Comments.
Share a thought or comment...
Comment 1 of 1

THis Code was updated 12 Years ago & it‘s still useful.

Thanks

Bapubsk Tips

Posted 101 days ago
 
Write a Comment...
...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.


Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P1245A1
Enter key:
KB Post Contributed By Bryan Valencia:

Bryan Valencia is the author of millions of lines of code and writes for PrestwoodBoards and Visual Studio Journey on-line magazines.

Visit Profile

 KB Article #101321 Counter
9643
Since 10/6/2008
Sales Website: www.prestwood.com Or visit our legacy sales site: 
legacy.prestwood.com


©1995-2020 Prestwood IT Solutions.   [Security & Privacy]