Skip to content

Instantly share code, notes, and snippets.

@patkremer
Created June 26, 2013 17:01
Show Gist options
  • Save patkremer/5869233 to your computer and use it in GitHub Desktop.
Save patkremer/5869233 to your computer and use it in GitHub Desktop.
Regular expressions in C#
using System;
using System.Text;
using System.Text.RegularExpressions;
namespace RegularExpressions
{
class BasicRegex
{
static void Main(string[] args)
{
string testString = "regular expressions are sometimes called regex or regexp";
Console.WriteLine("The test string is\n \"{0}\"", testString);
Console.Write("Match 'e' in the test string: ");
// Match 'e' in the test string
Regex expression = new Regex("e");
Console.WriteLine(expression.Match(testString));
Console.Write("Match every 'e' in the test string: ");
// match 'e' multiple times in the test string
foreach (var myMatch in expression.Matches(testString))
Console.Write("{0} ", myMatch);
Console.Write("\nMatch \"regex\" in the test string: ");
// match 'regex' in the test string
foreach (var myMatch in Regex.Matches(testString, "regex"))
Console.Write("{0} ", myMatch);
Console.Write("\nMatch \"regex\" or \"regexp\" using an optional 'p': ");
// use the ? quantifier to include an optional 'p'
foreach (var myMatch in Regex.Matches(testString, "regexp?"))
Console.Write("{0} ", myMatch);
// use alteration to match either cat or hat
expression = new Regex("(c|h)at");
Console.WriteLine("\n\"hat cat\" matches {0}, but \"cat hat\" matches {1}", expression.Match("hat cat"), expression.Match("cat hat"));
}
}
}
using System;
using System.Text.RegularExpressions;
using System.Text;
namespace CharacterClasses
{
class CharacterClasses
{
static void Main(string[] args)
{
string testString = "abc, DEF, 123";
Console.WriteLine("The test string is: \"{0}\"", testString);
// find the digits in the test string
Console.WriteLine("Match any digit");
DisplayMatches(testString, @"\d");
// find anything that isn't a digit
Console.WriteLine("\nMatch any nondigit");
DisplayMatches(testString, @"\D");
// find the word characters in the test string
Console.WriteLine("\nMatch any word character");
DisplayMatches(testString, @"\w");
// Find sequences of word characters
Console.WriteLine("\nMatch a group of at least one word character");
DisplayMatches(testString, @"\w+");
// Use a lazy quantifier
Console.WriteLine("\nMatch a group of at least one word character (lazy)");
DisplayMatches(testString, @"\w+?");
// match characters from a to f
Console.WriteLine("\nMatch anything from 'a' - 'f'");
DisplayMatches(testString, "[a-f]");
// Match anything that isn't in the range 'a' to 'f'
Console.WriteLine("\nMatch anything not from 'a' - 'f'");
DisplayMatches(testString, "[^a-f]");
// Match any sequence of letters in any case
Console.WriteLine("\nMatch a group of at least one letter");
DisplayMatches(testString, "[a-zA-Z]+");
// Use the . (dot) metacharacter to match any character
Console.WriteLine("\nMatch a group of any characters");
DisplayMatches(testString, ".*");
}
// display the matches to a regular expressions
private static void DisplayMatches(string input, string expression)
{
foreach (var regexMatch in Regex.Matches(input, expression))
Console.Write("{0} ", regexMatch);
Console.WriteLine(); // Move to next line
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment