Learn regular Expressions (JS, C #) _ Regular expressions

Source: Internet
Author: User
Tags readline

Use of regular expressions in JS

function Myvalid () {
  var errormsg = "";
  var res = true;
  Get the value you want to validate.
  var receivename = $ ("#receiveName"). Val ();//Name
  var receivemobile = $ ("#tMobile"). Val ()/mobile number
  var Validcode = $ ( "#validCode"). Val ()//Verification code

  var regname =/^[\u4e00-\u9fa5]{2,4}$/;//authentication name
  var regmobile =/^1[3|4|5|7|8][0-9]\ d{8}$/;//Verification Mobile phone
  var regcode =/^\d{4}$/;//Authentication Code
  if (!regname.test (Receivename)) {
  errormsg = "name format is incorrect; \n\r ";
  res = false;
  if (!regmobile.test (Receivemobile)) {
  errormsg + = "Cell phone number format is not correct; \n\r";
  res = false;
  if (!regcode.test (Validcode)) {
  errormsg = "Please enter a 4-bit digital authentication code; \n\r";
  res = false;
  if (!res) {
  $.ligerdialog.error (errormsg, error hint);
  }
  return res;
}

Regular expressions in C # use

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;

Using System.Threading.Tasks; Namespace regular Expression {using system.text.regularexpressions;//Regular Expression namespace class program {static void Main (string[] ar
       GS) {/* * Regular matching principle: * 1. Only focus on whether there is a string that can be matched without paying attention to its position and other parts that cannot be matched. * 2. Greedy mode: It will match as many as possible, which means it will match to the last string that can be matched. */while (true) {string str = Console.ReadLine ()
        ; The role of the #region meta character _ placeholder//method is to determine whether STR matches a custom regular expression rule//1.
        .: Represents a character placeholder, can represent any one character, must pass in a character, can be Chinese Console.WriteLine (Regex.IsMatch (str, "B.G")); 2. []: Represents a single character of a specified range, you can specify a discontinuous value, or you can specify a contiguous range: 0-9-Z-A-Z legal characters: 0-9-Z-Z-Console.WriteLine (Regex.IsMatch (str, "b[
        0-9]g "));
        In the regular, ^ can only be written at the beginning, if written in the middle, ^ is an ordinary character of the Console.WriteLine (Regex.IsMatch (str, "b[0-9a-z^a-z_]g"));
        Console.WriteLine (Regex.IsMatch (str, "b[^0-9a-za-z_^]g"));
      Here [0-z] contains all the characters between 0-z, and also contains some special symbols  Parsing the order of "b[9-1]g"-[XY] ranges is reversed.
        Can only be in the order of ASCII code from small to large Console.WriteLine (Regex.IsMatch (str, "b[9-1]g"));
        Console.WriteLine (Regex.IsMatch (str, "b[0-9-]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 a strict bit number match is involved, you need to add the start ^ and end $: represents a character that must be strictly matched from beginning to end Console.WriteLine (REGEX.ISMATC
        H (str, "^[0-9][0-9]$|^[0-9][0-9][0-9]$"));
        Included in [] is a plain character, not a metacharacters, which, in addition to the ^--need to be followed by the content, if the need to match, you can consider using escape \ Console.WriteLine (Regex.IsMatch (str, @ "^\^$")); 
        Console.WriteLine (Regex.IsMatch (str, "^\\^$")); #endregion #region Meta Character _ modifier//*: It is not a character placeholder, it means that it does not represent a character, it is used to decorate the preceding subexpression appears 0 or more times: The subexpression is the default refers to the character in front, if you need to represent
        Multiple, on the use () contains Console.WriteLine (Regex.IsMatch (str, "ab*g")); Console.WriteLine (Regex.IsMatch (str, "^ (AB) *g$")); +: It is not a character placeholder, it means that it does not represent a character, it is used to modify + the preceding subexpression appears 1 or more times: The subexpression is the default means + the preceding character, if you need to represent multiple, use () contains Console.WriteLine (
        Regex.IsMatch (str, "a+g")); //? : It is not a character placeholder, which means that it does not represent a character, it is used to decorate? The preceding subexpression appears 0 or 1 times: The subexpression is the default. The preceding character, if required, is used () to contain Console.WriteLine (
        Regex.IsMatch (str, "^a?g$")); {N,m}:{n,m} It is not a character placeholder, it means that it does not represent a character, it is used to decorate the preceding subexpression of {n,m} at least n times, up to M Times Console.WriteLine (Regex.IsMatch (str, "^[0-9
        ]{3,4}$ ")); {n}: It is not a character placeholder, stating that it does not represent a character, it is used to decorate the preceding subexpression of {n} only n times Console.WriteLine (Regex.IsMatch (str, "^[1-9][0-9]{17}$|^[
        0-9]{15}$|^[0-9]{17}[xx]$ ")); {N,}: It is not a character placeholder, stating that it does not represent a character, it is used to decorate {n,} The preceding subexpression appears at least n times, with no limit to Console.WriteLine (Regex.IsMatch str, "^[0-9]{3,}$"
        ));
        Console.WriteLine (Regex.IsMatch (str, "^q*$")); 
        #endregion//In C # determine if the login name contains special symbols: [^0-9a-za-z_]: Also cannot contain the Chinese #region shorthand 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: null characters: spaces, Tab stops, NewLine Console.WriteLine (Regex.IsMatch (str, @ "\s"));
        \s: Non-whitespace character Console.WriteLine (Regex.IsMatch (str, @ "\s"));
        \w:[0-9-Z-A-Z _ Chinese] Console.WriteLine (Regex.IsMatch (str, @ "\w"));
        \w: Special Symbol Console.WriteLine (Regex.IsMatch (str, @ "\w") other than [0-9-Z-A-Z _ Chinese]);
      #endregion}//Determine if the string is the correct domestic phone number, regardless of the extension. 
      010-8888888 or 010-88888880 or 010xxxxxxx//0335-8888888 or 0335-88888888 (area code-phone number)//10086, 10010, 95595, 95599, 95588 (5 bits)
        13888888888 (11 digits) while (true) {string str = Console.ReadLine (); Do not attempt to one-step, should be one to match Console.WriteLine (Regex.IsMatch (str,@ "^\d{3,4}[-]?\d{7,8}$|^[1-9]\d{4}$|^1[3-9]\d{9}$"))
      ;
        //Verify the legality of the user entered e-mail wuhu0723@126.com while (true) {string str = Console.ReadLine (); Do not attempt to one-step, should be one to match Console.WriteLine (Regex.IsMatch (str, @ "^[0-9a-za-z_]+[@][0-9a-za-z_]+[.] [A-za-z]
      {2,5}$ "));
 }
    }
  }
}

The above is about the full content of regular expressions, I hope to help you learn.

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.