text
stringlengths
0
13.4k
string input = "Hello World!";
string pattern = @"H.ll. W.rld!";
// true
return Regex.IsMatch(input, pattern, RegexOptions.IgnoreCase | RegexOptions.Singleline);
}
Section 15.4: Match into groups
public string Check()
{
string input = "Hello World!";
string pattern = @"H.ll. (?<Subject>W.rld)!";
Match match = Regex.Match(input, pattern);
// World
return match.Groups["Subject"].Value;
}
Section 15.5: Find all matches
Using
using System.Text.RegularExpressions;
Code
GoalKicker.com – .NET Framework Notes for Professionals
65
static void Main(string[] args)
{
string input = "Carrot Banana Apple Cherry Clementine Grape";
// Find words that start with uppercase 'C'
string pattern = @"\bC\w*\b";
MatchCollection matches = Regex.Matches(input, pattern);
foreach (Match m in matches)
Console.WriteLine(m.Value);
}
Output
Carrot
Cherry
Clementine
Section 15.6: Simple match and replace
public string Check()
{
string input = "Hello World!";
string pattern = @"W.rld";
// Hello Stack Overflow!
return Regex.Replace(input, pattern, "Stack Overflow");
}
GoalKicker.com – .NET Framework Notes for Professionals
66
Chapter 16: File Input/Output
Parameter
string path Path of the file to check. (relative or fully qualified)
Details
Section 16.1: C# File.Exists()
using System;
using System.IO;
public class Program
{
public static void Main()
{
string filePath = "somePath";
if(File.Exists(filePath))
{
Console.WriteLine("Exists");
}
else
{
Console.WriteLine("Does not exist");
}
}
}
Can also be used in a ternary operator.
Console.WriteLine(File.Exists(pathToFile) ? "Exists" : "Does not exist");
Section 16.2: VB WriteAllText