using System;
using system.Text.RegularExpressions;
public class Example {public static void main() {string pattern = @"\b(\p{IsGreek}+(\s)?)+\d{Pd}\s(\p{IsBasicLatin}+(\s)?)+";string input = "Κατα Μαθθα?ον - The Gospel of Matthew";Console.WriteLine(Regex.IsMatch(input, pattern));}
}
使用
using System;
using System.Text.RegularExpressions;
public class Example {public static void Main() {string pattern = @"(\P{Sc})+";string[] values = {"$163,901.78", "f1,073,143.68", "73¢","€120" };foreach(string value in values)Console.WriteLine(Regex.Match(value, pattern).Value);}
}
// The example displays the following output:
// 164,091.78
// 1,073,142.68
// 73
// 120
使用\w語言匹配單詞中的重復字符,\1匹配第一次捕獲的值
using System;
using System.Text.RegularExpressions;public class Example
{public static void Main(){string pattern = @"(\w)\1";string[] words = { "trellis", "seer", "latter", "summer","hoarse", "lesser", "aardvark", "stunned" };foreach (string word in words){Match match = Regex.Match(word, pattern);if (match.Success)Console.WriteLine($"'{match.Value}' found in '{word}' at position {match.Index}.");elseConsole.WriteLine($"No double characters in '{word}'.");}}
}
// The example displays the following output:
// 'll' found in 'trellis' at position 3.
// 'ee' found in 'seer' at position 1.
// 'tt' found in 'latter' at position 2.
// 'mm' found in 'summer' at position 2.
// No double characters in 'hoarse'.
// 'ss' found in 'lesser' at position 2.
// 'aa' found in 'aardvark' at position 0.
// 'nn' found in 'stunned' at position 3.