Last active
August 29, 2015 14:05
-
-
Save niisar/497b3b7fc79de56118ac to your computer and use it in GitHub Desktop.
regex csharp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Text.RegularExpressions; | |
namespace regex_ | |
{ | |
class Program | |
{ | |
private static void showMatch(string text, string expr) | |
{ | |
Console.WriteLine("The Expression: " + expr); | |
MatchCollection mc = Regex.Matches(text, expr); | |
foreach (Match m in mc) | |
{ | |
Console.WriteLine(m); | |
} | |
} | |
//matches words that start with 'S': | |
private static void showMatch() | |
{ | |
string str = "A Thousand Splendid kingSup Suns"; | |
Console.WriteLine("Matching words that start with 'S': "); | |
showMatch(str, @"\bS\S*"); | |
Console.ReadKey(); | |
} | |
// matches words that start with 'm' and ends with 'e': | |
private static void showMatchStartEnd() | |
{ | |
string str = "make maze and manage to measure it"; | |
Console.WriteLine("Matching words start with 'm' and ends with 'e':"); | |
showMatch(str, @"\bm\S*e\b"); | |
Console.ReadKey(); | |
} | |
//replace extra white space | |
private static void regReplace() | |
{ | |
string input = "Hello World "; | |
string pattern = "\\s+"; | |
string replacement = " "; | |
Regex rgx = new Regex(pattern); | |
string result = rgx.Replace(input, replacement); | |
Console.WriteLine("Original String: {0}", input); | |
Console.WriteLine("Replacement String: {0}", result); | |
Console.ReadKey(); | |
} | |
static void Main(string[] args) | |
{ | |
regCaseInsencitive(); | |
Console.ReadKey(); | |
} | |
private static void mathParticularString() | |
{ | |
// First we see the input string. | |
string input = "/content/alternate-1.aspx"; | |
// Here we call Regex.Match. | |
Match match = Regex.Match(input, @"content/([A-Za-z0-9\-]+)\.aspx$", | |
RegexOptions.IgnoreCase); | |
// Here we check the Match instance. | |
if (match.Success) | |
{ | |
// Finally, we get the Group value and display it. | |
string key = match.Groups[1].Value; | |
Console.WriteLine(key); | |
} | |
Console.ReadKey(); | |
} | |
private static void regCaseInsencitive() | |
{ | |
//Using ToLower instead of RegexOptions.IgnoreCase on the Regex yielded a 10% or higher improvement. | |
string input = "/content/alternate-1.aspx"; | |
// Here we lowercase our input first. | |
input = input.ToLower(); | |
Match match = Regex.Match(input, @"content/([A-Za-z0-9\-]+)\.aspx$"); | |
if (match.Success) | |
{ | |
string key = match.Groups[1].Value; | |
Console.WriteLine(key); | |
} | |
Console.ReadKey(); | |
} | |
///tips | |
//You can add the RegexOptions.Compiled flag for a substantial performance gain at runtime. This will however make your program start up slower. With RegexOptions.Compiled we see often 30% better performance. | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
good tutorial in regex
http://www.codeproject.com/Articles/9099/The-Minute-Regex-Tutorial