using System;
using System.Text.RegularExpressions;
class MainClass
{
public static void Main(string[] args)
{
string input = "Hooray for Regex! It's a match made in code heaven!";
string pattern = @"\bmatch\b";
// Check if the word "match" is in the input string
bool isMatch = Regex.IsMatch(input, pattern);
Console.WriteLine(isMatch); // Output: True
}
}
using System;
using System.Text.RegularExpressions;
class MainClass
{
public static void Main(string[] args)
{
string input = @"Name: Jane Smith
Location: New York, NY
Email: jane.smith@email.com";
string pattern = @"Name:\s+(?<name>.+)
Location:\s+(?<location>.+)
Email:\s+(?<email>\S+@\S+\.\S+)";
RegexOptions options = RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace;
Match match = Regex.Match(input, pattern, options);
if (match.Success)
{
Console.WriteLine($"Name: {match.Groups["name"].Value}");
Console.WriteLine($"Location: {match.Groups["location"].Value}");
Console.WriteLine($"Email: {match.Groups["email"].Value}");
}
}
}
using System;
using System.Text.RegularExpressions;
class MainClass
{
public static void Main(string[] args)
{
string input = "I was born on 2001-05-15 and started my job on 2020-01-01.";
string pattern = @"(\d{4})-(\d{2})-(\d{2})";
string replacement = "$3-$2-$1";
// Replace dates with the new format
string output = Regex.Replace(input, pattern, replacement);
Console.WriteLine(output); // Output: I was born on 15-05-2001 and started my job on 01-01-2020.
}
}
using System;
using System.Text.RegularExpressions;
class MainClass
{
public static void Main(string[] args)
{
string input = "Regex can do wonders for your text manipulation skills!";
string pattern = @"\b\w{4,}\b";
// Find and output all words containing 4 or more characters
MatchCollection matches = Regex.Matches(input, pattern);
foreach (Match match in matches)
{
Console.WriteLine(match.Value);
}
}
}
//Capturing group example
string input = "The color is red.";
Match match = Regex.Match(input, "The color is (red|blue)");
Console.WriteLine(match.Groups[1].Value); // Output: red
//Non-capturing group example
input = "The color is red.";
match = Regex.Match(input, "The color is (?:red|blue)");
Console.WriteLine(match.Groups[0].Value); // Output: The color is red.
string input = "I want to buy a car and a bike";
string pattern = @"\ba(?:\w+)(?= a\b)";
MatchCollection matches = Regex.Matches(input, pattern);
foreach (Match match in matches)
{
Console.WriteLine(match.Value);
}
// Output:
// car
負先行(?!...)確保先行內的模式不存在於字符串中,但不會消耗字符串中的字符。
input = "I want to buy a car and a bike";
pattern = @"\ba(\w+)(?! a\b)";
matches = Regex.Matches(input, pattern);
foreach (Match match in matches)
{
Console.WriteLine(match.Value);
}
// Output:
// bike
input = "100 kg of apples and 200 kg of oranges";
pattern = @"(?<=\d{3}\s)kg";
matches = Regex.Matches(input, pattern);
foreach (Match match in matches)
{
Console.WriteLine(match.Value);
}
// Output:
// kg
string input = "123/apple 456/orange";
string pattern = @"(?<!123/)apple";
Match match = Regex.Match(input, pattern);
Console.WriteLine(match.Value);
// Output: (no match found)
string pattern =
@"^ # Start of the line
(?<header>[\w\s]+) # Header (word and whitespace characters)
: # Literal colon separator
(?<content>.+?) # Content (any non-newline chars)
$ # End of the line";
string logText = "2021-08-01 12:00:00 - ERROR - Could not connect to database.";
string regexPattern = @"\bERROR\b\s-\s(.+)";
Match match = Regex.Match(logText, regexPattern);
Console.WriteLine(match.Groups[1].Value); // Output: Could not connect to database.
string code = "using System;\nnamespace HelloWorld{class Program{static void Main(string[] args){Console.WriteLine(\"Hello, World!\");}}}";
// Regex pattern to find "Console.WriteLine" method and find all matches
MatchCollection matches = Regex.Matches(code, @"Console\.WriteLine\(");
// Print the number of matches found
Console.WriteLine($"Found {matches.Count} matches.");
日誌文件分析與調試:
通過使用正則表達式模式,您可以解析日誌文件以查找特定的錯誤消息或發生的情況,從而幫助調試過程。
string logContents = "2012-07-12 INFO Success\n2012-07-15 ERROR Failure";
string errorPattern = "^.*ERROR.*$";
MatchCollection errorMatches = Regex.Matches(logContents, errorPattern, RegexOptions.Multiline);
foreach (Match match in errorMatches)
{
Console.WriteLine(match.Value);
}
在 ASP.NET 中使用 Regex C# 進行表單驗證和輸入清理
正則表達式在 Web 應用程序中的表單驗證和輸入清理中發揮著至關重要的作用。掌握 C# 中的正則表達式可讓您創建強大且安全的 ASP.NET 應用程序,從而有效驗證用戶輸入、確保數據完整性並防止潛在的安全問題。例如,您可以將正則表達式與驗證器控件(例如 )結合使用RegularExpressionValidator,以匹配用戶提交的輸入中的特定模式。
string input = "Contact Alice at alice@example.com and Bob at bob@example.org";
string emailPattern = @"\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}\b";
MatchCollection emailMatches = Regex.Matches(input, emailPattern, RegexOptions.IgnoreCase);
foreach (Match match in emailMatches)
{
Console.WriteLine(match.Value);
}
// Output:
// alice@example.com
// bob@example.org