Some people like to say there are two kinds of regular expressions, but this is nonsense and a gross oversimplification. One reference claims there are 418 "regex flavors" and the count is probably growing every day. The good news is that the flavors all are mostly the same, except in details. Even in cases where that is not true, the basic concepts remain the same.
First of all, you will need this near the top of your program:
using System.Text.RegularExpressions;
if ( Regex.IsMatch ( line, @"^[^0-9]" ) ) Console.WriteLine ( "The string matches." );Another way is like this:
static string pattern = @"^[^0-9]"; Regex rx = new Regex(pattern); if ( rx.IsMatch ( line ); Console.WriteLine ( "The string matches." );The first way uses a class method, with no need to create a Regex object.
static string original = "I have an ugly dog"; string result = Regex.Replace ( original, "dog", "cat" );Here the regex is nothing fancy, just the word "dog" -- which gets replaced with the word "cat".
using System; using System.Text.RegularExpressions; namespace HogHeaven { public class Program { public static void Main(string[] args) { string original = "I have an ugly dog"; Regex rx = new Regex ( "dog" ); string result = rx.Replace ( original, "cat" ); Console.WriteLine ( result ); } } }This yields the output "I have an ugly cat".
rego "he is ugly" he MaynardAnd you would get "Maynard is ugly". Or you could use fancier regex such as:
rego "Sally is ugly" u.* niceAnd get: "Sally is nice"
It is worth a peek at the following to see how command line arguments are handled. Note that C# does not place the program name as the first argument like C would do.
using System; using System.Text.RegularExpressions; namespace HogHeaven { public class Program { public static void Main(string[] args) { if ( args.Length != 3 ) { Console.WriteLine ( "Usage: rego string regex repl" ); return; } string original = args[0]; Regex rx = new Regex ( args[1] ); string result = rx.Replace ( original, args[2] ); Console.WriteLine ( result ); } } }This little program would be a way to experiment with C# regex in any way you might want.
Tom's Computer Info / [email protected]