Example 365 (10) --------- common character verification techniques for Regular Expressions

Source: Internet
Author: User

I. use regular expressions to check repeated words in a string,

Ii. Code
Using System; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. text; using System. windows. forms; namespace ValidateWord {public partial class Frm_Main: Form {public Frm_Main () {InitializeComponent ();} private void button#click (object sender, EventArgs e) {/* \ escape character, escape a character with special functions into a common character, or in turn match the start position of the input string $ match the end position of the input string * match the previous zero or multiple When the sub-expression + matches the previous one or more sub-expressions? Match the previous zero or one subexpression {n} n is a non-negative integer. Match the previous n subexpressions {n,} n is a non-negative integer, at least match the previous n subexpressions {n, m} m and n are non-negative integers, where n <= m, at least n times and at most m times? When this character is followed by other delimiters (*, + ,?, If {n}, {n ,}, {n, m}) is followed, the matching mode matches as few strings as possible. match any single character except "\ n" (pattern) to match pattern and obtain this match (?: Pattern) matches pattern but does not get the matching result (? = Pattern) Forward pre-query. Match the search string (?! Pattern) negative pre-query. If the string does not match pattern, match the search string x | y to match x or y. For example, 'z | food' can match "z" or "food ". '(Z | f) ood' matches "zood" or "food" [xyz] character set. Match any character in it. For example, '[abc]' can match 'A' [^ xyz] negative value character set in 'plain. Match any character not included. For example, '[^ abc]' can match 'P' [a-z] In 'plain 'to match any character in the specified range. For example, '[a-z]' can match any lowercase letter character [^ a-z] within the range of 'A' to 'Z. For example, '[^ a-z]' can match a value other than 'A '~ Any character \ B in 'z'' matches a word boundary. It indicates the position between the word and the space \ B matches non-word boundary \ d matches a digit character, it is equivalent to [0-9] \ D matching a non-numeric character, it is equivalent to [^ 0-9] \ f matching a linefeed \ n matching a linefeed \ r matching a carriage return \ s matching any blank characters, including spaces, tabs, and page breaks. \ S matches any non-blank characters. \ t matches a tab. \ v matches a vertical tab. It is equivalent to matching any word characters including underlines with \ x0b and \ cK \ w. It is equivalent to ''[A-Za-z0-9 _] '\ W matching any non-word character. It is equivalent to '[^ A-Za-z0-9 _]' \ k <name> named back reference. For example ,(? <Char> \ w) \ k <char> searches for double-write word characters. Expression (? <43> \ w) \ 43. You can use single quotes to replace angle brackets, for example, \ k'char '. (? <Name> subexpression) captures the matched subexpression to a group name or number name. The string used for name cannot contain any punctuation marks and cannot start with a number. You can use single quotes to replace angle brackets, such (? 'Name '). */System. Text. RegularExpressions. MatchCollection matches = // use a regular expression to search for the System. Text. RegularExpressions. Regex. Matches (label1.Text, @ "\ B (? <Word> \ w +) \ s + (\ k <word>) \ B ", System. text. regularExpressions. regexOptions. compiled | System. text. regularExpressions. regexOptions. ignoreCase); if (matches. count! = 0) // If the set contains content {foreach (System. text. regularExpressions. match // traverse the set match in matches) {string word = match. groups ["word"]. value; // obtain the duplicate message box. show (word. toString (), "English word"); // The message dialog box is displayed.} else {MessageBox. show ("No duplicate words");} // pop-up message dialog box} private void Form1_Load (object sender, EventArgs e) {label1.Text = // create a String object "The quick brown fox jumped over the lazy dog. ";}}}
3. Use a regular expression to replace a string,

4. Code
Using System; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. text; using System. windows. forms; namespace ReplaceStr {public partial class Frm_Main: Form {public Frm_Main () {InitializeComponent ();} private void button#click (object sender, EventArgs e) {/* input type: System. string: the String to be matched. Pattern type: the regular expression pattern to be matched by System. String. Replacement type: System. String replacement String. */String strResult = System. Text. RegularExpressions. Regex. // Replace the string Replace (textBox1.Text, @ "[A-Za-z] \ *? ", TextBox2.Text); MessageBox. show ("replace Prefix:" + "\ n" + textBox1.Text + // the character to be replaced by "\ n" + "in the pop-up message dialog box: "+" \ n "+ textBox2.Text +" \ n "+" character after replacement: "+" \ n "+ strResult," replace ");} private void button2_Click (object sender, EventArgs e) {Close (); // Close the form }}}
5. Use regular expressions to split strings,

Vi. Code
Using System; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. text; using System. windows. forms; namespace ValidateSplit {public partial class Frm_Main: Form {public Frm_Main () {InitializeComponent ();} private void btn_Split_Click (object sender, EventArgs e) {string [] P_Str = System. text. regularExpressions. // use a regular expression to split the Regex according to the number. split (txt_Split.Text, "[1-9]"); foreach (string s in P_Str) // traverses the Split string set {txt_Result.Text + = s; // display string} private void btn_Close_Click (object sender, EventArgs e) {Application. exit (); // Exit the application }}}
7. Use regular expressions to verify the input letters,

8: Code
Using System; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. text; using System. windows. forms; namespace IsLetter {public partial class Frm_Main: Form {public Frm_Main () {InitializeComponent ();} private void btn_Validate_Click (object sender, EventArgs e) {if (! IsLetter (textBox1.Text. Trim () // check whether the string is composed of uppercase and lowercase letters {MessageBox. Show ("the input is not a letter !!! "," Prompt ") ;}// the pop-up message dialog box else {MessageBox. Show (" the input is a letter !!!!! "," Prompt ");} // pop-up message dialog box} // <summary> // verify whether the string is composed of uppercase and lowercase letters /// </summary> /// <param name = "str_Letter"> string </param> // <returns> returns a Boolean value </returns> public bool IsLetter (string str_Letter) {return System. text. regularExpressions. regex. // use A regular expression to determine if IsMatch (str_Letter, @ "^ [A-Za-z] + $ ");}}}
9. Verification of other common characters

Use regular expressions to verify Chinese Character Input @ "^ [\ u4e00-\ u9fa5], {0,} $"

Use a regular expression to verify whether the length of the input string is greater than 8 @ "^. {8,} $"

Use regular expressions to verify the E-mail format @ "^ ([\ w-\.] +) @ (\ [0-9] {1, 3 }\. [0-9] {1, 3 }\. [0-9] {1, 3 }\.) | ([\ w-] + \.) +) ([a-zA-Z] {2, 4} | [0-9] {1, 3}) (\]?) $ ")

Use a regular expression to verify the IP address "(25 [0-5] | 2 [0-4] \ d | [0-1] \ d {2} | [1-9 ]? \ D )"

Use a regular expression to verify URL @ "http (s )? : // ([\ W-] + \.) + [\ w-] + (/[\ w -./? % & =] *)? "

Related Article

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.