Learn Regular Expressions (js, C #),

Source: Internet
Author: User

Learn Regular Expressions (js, C #),

Use regular expressions in js

Function myValid () {var errorMsg = ""; var res = true; // get the value to be verified. var upload ename = $ ("# upload ename "). val (); // name var receiveMobile =$ ("# tMobile "). val (); // mobile phone number var validCode = $ ("# validCode "). val (); // Verification Code var regName =/^ [\ u4e00-\ u9fa5] {2, 4} $ /; // verify the name var regMobile =/^ 1 [3 | 4 | 5 | 7 | 8] [0-9] \ d {8} $ /; // verify the mobile phone var regCode =/^ \ d {4} $/; // verification code if (! RegName. test (canonical ename) {errorMsg + = "the name format is incorrect; \ n \ r"; res = false;} if (! RegMobile. test (receiveMobile) {errorMsg + = "Incorrect mobile phone number format; \ n \ r"; res = false;} if (! RegCode. test (validCode) {errorMsg + = "enter a four-digit Verification Code; \ n \ r"; res = false;} if (! Res) {$. ligerDialog. error (errorMsg, "error prompt");} return res ;}

Use the Regular Expression in C #

Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; namespace Regular Expression {using System. text. regularExpressions; // The namespace class Program {static void Main (string [] args) of the regular expression {/** Regular Expression matching principle: * 1. only focus on whether a string can be matched, not its location, and other unmatched parts. * 2. greedy mode: the regular expression matches as many strings as possible, which means that it matches the last string that can be matched */while (true) {string str = Console. readLine (); # region metacharacter _ placeholder // the function of the method is to determine whether str matches a custom regular expression rule // 1 ..: represents a placeholder character. It can represent any character. One character must be input. It can be a Chinese Console. writeLine (Regex. isMatch (str, "B. g "); // 2. []: represents a single character in a specified range, you can specify the value of the break, you can also specify the continuous range: 0-9 A-Z a-z valid characters: 0-9 A-Z a-z _ Console. writeLine (Regex. isMatch (str, "B [0-9] g"); // in the regular expression, ^ can only be written at the beginning. If it is written in the middle, ^ is a common character of the Console. W RiteLine (Regex. isMatch (str, "B [0-9A-Z ^ a-z _] g"); Console. writeLine (Regex. isMatch (str, "B [^ 0-9A-Za-z _ ^] g"); // here [0-z] will contain all characters between 0 and Z, it also contains some special symbols // the order in which the range of "B [9-1] g"-[x-y] is being analyzed is reversed. The Console can only be small to large in the order of ASCII codes. writeLine (Regex. isMatch (str, "B [9-1] g"); Console. writeLine (Regex. isMatch (str, "B [0-] g"); // 11-19 Console. writeLine (Regex. isMatch (str, "[11-19]"); // 1 1-1 9 Console. writeLine (Regex. isMatch (str, "B [^ 0-9] g"); // |: or Console. writeLine (Regex. isMatch (str, "[0-9] [0-9] | [0-9] [0-9] [0-9]"); // hjasdgf1234fjhadsjfhas // If strict bid matching is involved, you need to add start ^ and end $: indicates that the character must strictly match the regular Console from start to end. WriteLine (Regex. isMatch (str, "^ [0-9] [0-9] $ | ^ [0-9] [0-9] [0-9] $ ")); // All characters contained in [] are normal characters, not metacharacters. Except ^-requires content to be followed. If matching is required, you can consider using escape \ Console. writeLine (Regex. isMatch (str, @ "^ \ ^ $"); Console. writeLine (Regex. isMatch (str, "^ \ ^ $"); # endregion # region metacharacters _ modifier // *: it is not a placeholder character, it does not represent a single character. It is used to modify * when the first subexpression appears 0 or multiple times: the subexpression is the character before * by default, if you need to represent multiple, use () to include the Console. writeLine (Regex. isMatch (str, "AB * g"); Console. writeLine (Reg Ex. isMatch (str, "^ (AB) * g $"); // +: it is not a placeholder, meaning it does not represent a character, it is used to modify the subexpression that appears once or multiple times before +: the subexpression refers to the character before + by default. To represent multiple, use () contains Console. writeLine (Regex. isMatch (str, "a + g "));//?: It is not a placeholder character, indicating that it does not represent a character, it is used to modify? The previous subexpression appears 0 or 1 time: What is the default value of the subexpression? The character above, if it needs to represent multiple, uses () to include Console. WriteLine (Regex. IsMatch (str, "^? G $ "); // {n, m }:{ n, m} It is not a placeholder character, meaning it does not represent a character, it is used to modify {n, m} the subexpression in front of the Console appears at least n times and at most m times. writeLine (Regex. isMatch (str, "^ [0-9] {3, 4} $"); // {n}: it is not a placeholder, meaning it does not represent a character, it is used to modify the subexpression before {n} and can only appear n times on the Console. writeLine (Regex. isMatch (str, "^ [1-9] [0-9] {17} $ | ^ [0-9] {15} $ | ^ [0-9] {17} [xX] $ ")); // {n ,}: it is not a placeholder character, indicating that it does not represent a character. It is used to modify the subexpression before {n ,} to appear at least n times, the Console is not limited at most. writeLine (Regex. isMatch (str, "^ [0-9] {3 ,}$"); Console. write Line (Regex. isMatch (str, "^ q * $"); # endregion // check whether the logon name contains a special symbol in c #: [^ 0-9A-Za-z _]: it cannot contain Chinese # region short expression // \ d: represents a number, equivalent to [0-9] Console. writeLine (Regex. isMatch (str, @ "^ \ d {3 ,}$"); // \ D: Non-digital Console. writeLine (Regex. isMatch (str, @ "\ D"); // \ s: Empty character: space, tab, line feed Console. writeLine (Regex. isMatch (str, @ "\ s"); // \ S: Non-blank character Console. writeLine (Regex. isMatch (str, @ "\ S"); // \ w: [0-9 a-z A-Z _ Chinese] Console. writeLine (Rege X. isMatch (str, @ "\ w"); // \ W: special symbol Console except for [0-9 a-z A-Z _ Chinese. writeLine (Regex. isMatch (str, @ "\ W"); # endregion} // checks whether the string is a correct domestic phone number, regardless of the extension. // 010-8888888, 010-88888880, or 010 xxxxxxx // 0335-8888888 or 0335-88888888 (area code-phone number) // 10086, 10010, 95595, 95599, 95588 (5 digits) // 13888888888 (11 digits) while (true) {string str = Console. readLine (); // do not try the steps in place. Match the Console one by one. writeLine (Regex. isMatch (str, @ "^ \ d {3, 4} [-]? \ D {7, 8 }$ | ^ [1-9] \ d {4 }$ | ^ 1 [3-9] \ d {9} $ "));} // verify the validity of the email address entered by the user wuhu0723@126.com while (true) {string str = Console. readLine (); // do not try the steps in place. Match the Console one by one. writeLine (Regex. isMatch (str, @ "^ [0-9A-Za-z _] + [@] [0-9A-Za-z _] + [.] [A-Za-z] {2, 5} $ "));}}}}

The above is all about the regular expression and I hope it will be helpful for your learning.

Articles you may be interested in:
  • Javascript mobile phone number Regular Expression verification function
  • Javascript Regular Expression Form Verification Code
  • Detailed explanation of the js Code of the regular expression in the verified email address
  • Practical JS Regular Expressions (mobile phone number/IP address Regular Expression/zip code Regular Expression/phone number)
  • Use of js Regular Expressions
  • Regular expression syntax rules and usage in Javascript and C #
  • JS Regular Expressions (detailed and practical)
  • Verify the digital code using the JS Regular Expression
  • Matching strings for js Regular Expression learning notes
  • Use regular expressions in js and C # For instance analysis to match a tag

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.