Click here to Skip to main content
15,894,907 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
What is the use of regex in dotnet and how can we implement this in our program?
Posted
Updated 10-Jul-13 3:21am
v2

Regexes (or Regular Expressions) are a way to process text and perform a variety of pattern matching processes on them.

And example would be to write an application to rename all your MP3 files to have a "MY MUSIC" prefix:
C#
public static Regex regex = new Regex(
      "^(?<!MYMUSIC)(.*\\.MP3)$",
    RegexOptions.IgnoreCase
    | RegexOptions.Multiline
    | RegexOptions.Singleline
    | RegexOptions.IgnorePatternWhitespace
    | RegexOptions.Compiled
    );


// This is the replacement string
public static string regexReplace =
      "MYMUSIC$1";


// Replace the matched text in the InputText using the replacement pattern
 string result = regex.Replace(InputText,regexReplace);
 
Share this answer
 
Comments
Thomas Daniels 10-Jul-13 9:22am    
Great example, +5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900