C # Regular Expressions

Source: Internet
Author: User

When it comes to C # regular expressions, the title is a bit biased, the regular expression is supported by all languages, not just C #, but the definition of regular expressions is varied and does not achieve different effects:

Basic knowledge of regular expressions:

/****************************************** Common Meta-character ********************************************************/

Code Description
. Match any character other than line break
\w Match letters or numbers or underscores or kanji
\s Match any of the whitespace characters
\d Match numbers
\b Match the beginning or end of a word
^ Match the start of a string
$ Match the end of a string

/******************************************* Common restriction ******************************************************/

Code/Syntax Description
* Repeat 0 or more times
+ Repeat one or more times
? Repeat 0 or one time
N Repeat n times
{N,} Repeat N or more times
{N,m} Repeat N to M times

/******************************************* commonly used antonyms *******************************************************/

match is not the beginning or end of a word
code/syntax description
\w matches any characters that are not letters, numbers, underscores, kanji
\s
\d< /span> matches any non-numeric character
\b
[^x] matches any character except x
[^aeiou] matches any character except aeiou these letters

/****************************************** Practical Code Reference ********************************************************/

Common function points for regular expressions:

1, matching elements grab the desired results, generally used for data collection and web analysis;

2, data validation;

Simple examples of the use of regular expressions:

Code implementation Function Description:
Verify that the user input data format is: 23:15-23:00 or 23:15-23:00;23:15-23:00;23:15-23:00 data format

The code is as follows: (regular expression is the focus)

 Static voidMain (string[] args) {MatchCollection Martcollcect=NewRegex (@"(^\d{2}:\d{2}-\d{2}:\d{2};$)"). Matches ("23:15-25:16;23:15-25:16;23:15-25:16"); BOOLResultbook = Regexchecktime ("23:15-25:16;23:15-25:16;23:15-25:16"); Resultbook= Regexchecktime ("23:15-23:16;as:15-23:16;23:15-23:16"); Resultbook= Regexchecktime ("23:15-23:16;23:15-23:16;23:15-22:16"); Resultbook= Regexchecktime ("23:15-23:16"); }        /// <summary>        ///The validation time format is: 23:15-25:16;03:15-25:16 mode/// </summary>        /// <param name= "Inputcontext" ></param>        /// <returns></returns>         Public Static BOOLRegexchecktime (stringInputcontext) {            stringRegparm =@"^ ([[0-1]\d|2[0-3]):([0-5]\d]-([0-1]\d|2[0-3]):([0-5]\d];) * ([0-1]\d|2[0-3]):([0-5]\d]-([0-1]\d|2[0-3]):([ 0-5]\d) $"; Regex RG=NewRegex (regparm); returnRG.        IsMatch (Inputcontext); }

Regular reference syntax:
Http://www.cnblogs.com/youring2/archive/2009/11/07/1597786.html

C # Regular Expressions

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.