Regular Expressions are text patterns that are often used to match texts with certain rules. Regular Expressions can be very complex, and the content can be very advanced, but common rules are very simple. This article mainly summarizes the recently used regular expression syntax, which is sufficient to meet common small demands.
First, let's take a look at the basic unit: special characters and metacharacters. Http://msdn.microsoft.com/zh-cn/library/ae5bf541 (V = vs.100). aspx this article has a detailed reference list.
A special character means that it does not match a certain word, but it has auxiliary meanings. For example, ^ indicates that the current mode must start with the text, and $ indicates the end of the text, + indicates that this mode appears once or multiple times.
Metacharacter indicates matching a word. For example, \ D indicates a number, that is, [0-9], and \ W indicates a-Z, a-Z, 0-9, and underline.
After getting familiar with special characters and metacharacters, let's take a look at some examples.
To match a time string in a fixed format like: @ "\ D + 4 \-\ D {1, 2} \-\ D {1, 2}" is preceded by the symbol "@" C.
To match a time format similar to "Sep 4": @ "(Jan | Feb | Mar | APR | May | jun | Jul | Aug | Sep | Oct | Nov | dec) \ s \ D {1, 2 }"
The last example is a C # instance.
1 class Program 2 { 3 static void Main(string[] args) 4 { 5 Regex regex = new Regex(@"^\(CNN\) (Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday)[\s|\S]*\.$"); 6 string content = @"(CNN) Tuesday he coalition that attacked ISIS in Syria overnight ""makes it clear to the world that this is not America‘s fight alone,"" U.S. President Barack Obama said Tuesday."; 7 string content2= @"(CNN) Tuesday he coalition that attacked ISIS in Syria overnight ""makes it clear to the world that this is not America‘s fight alone,"" U.S. President Barack Obama said Tuesday.hehe"; 8 Console.WriteLine(regex.IsMatch(content)); 9 Console.WriteLine(regex.IsMatch(content2));10 12 Console.ReadKey();13 14 }15 }
Regular Expression Summary