Regular Expression Summary

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.