How regular expressions are used in C #

Source: Internet
Author: User

"Draft version, read carefully"

Reference Document: Regular Expression 30-minute introductory tutorial

For systematic learning of regular expression content, please visit the tutorial above.

The regular expression matches a character or string according to the specified rule. ‘.‘ ' \b ' \d ' et cetera is called a meta-character in a regular expression, such as a dot character used to match any character other than a newline, \b metacharacters are used to match words, \s metacharacters are used to match spaces, and so on.

Regular expression conversions:

Show You the Code: (\s)

1             stringinput ="I am Lily.";2             stringPattern =@"\s+";3             stringReplacement =" ";4             stringresult =Regex.Replace (input, pattern, replacement);5 6Console.WriteLine ("Original String: {0}", input);7Console.WriteLine ("Replacement String: {0}", result);

\s is used to match spaces, while \s+ is used to match one or more and a space, as shown in the code, to replace one or more spaces with a space, running the effect

Show You the Code: (\b)

1             stringinput ="I am Lily.";2             stringPattern =@"\bis\b";3             stringReplacement ="am";4             stringresult =Regex.Replace (input, pattern, replacement);5Console.WriteLine ("Original String: {0}", input);6Console.WriteLine ("Replacement String: {0}", result);

\b is used to match words, the \bis\b is used to match the word is, and is replaced with AM, running the effect

Show You the code: (. *)

input ="Sis a Girld"; Pattern=@"Is\b.*\bgirl"; Replacement="*"; Result=Regex.Replace (input, pattern, replacement); Console.WriteLine ("Original String: {0}", input); Console.WriteLine ("Replacement String: {0}", result);

. * Used to match any number of non-newline characters, the pattern match is preceded by the IS Girl end, and is behind and girl must precede a space string, run effect

Verify that the regular expression matches:

Show You the Code: (\d)

1             stringPhone ="";2             stringPattern =@"1\d{10}";3             BOOLresult =false;4Console.WriteLine ("Please enter your mobile phone number.");5              Do6             {7Phone =console.readline ();8result =Regex.IsMatch (phone, pattern);9                 if(!result)TenConsole.WriteLine ("Badnum,try again"); One} while(!result); AConsole.WriteLine ("Good");

\d is used to match a single digit, the d{10} is used to match 10 digits and runs as follows:

  

Continuous update ...

How regular expressions are used in C #

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.