文章目录
  1. 1. 什么是正则表达式
  2. 2. 基本语法
  3. 3. 实例

在编程过程中,经常会使用到正则表达式,每次用到都是去网上查找资料。。实在太烦了,这里就简单整理一下它的语法。

什么是正则表达式

正则表达式是一种文本模式,包括普通字符(例如,a 到 z 之间的字母)和特殊字符(称为“元字符”)。模式描述在搜索文本时要匹配的一个或多个字符串。

基本语法

  • 以 / 开始和结尾

    "/abc/" matches any string containing "abc""abc", "abcdef", "defabc", ".=.abc.=.", ...
    
  • . 匹配除“\n”之外的任何单个字符

    "/.oo.y/" matches "Doocy", "goofy", "LooNy", ...
    
  • A trailing i at the end of a regex (after the
    closing /) signifies a case-insensitive match。

    "/mart/i" matches "Marty Stepp", "smart fellow", "WALMART", ...
    
  • | means OR

    "/abc|def|g/" matches "abc", "def", or "g"
    
  • () are for grouping

    "/(Homer|Marge) Simpson/" matches "Homer Simpson" or "Marge Simpson"
    
  • ^ matches the beginning of a line; $ the end

    "/^<!--$/" matches a line that consists entirely of "<!--"
    
  • \ starts an escape sequence

    "/<br \/>/" matches lines containing <br /> tags
    
  • 星号* means 0 or more occurrences
    "/abc*/" matches "ab", "abc", "abcc", "abccc", ...
    "/a(bc)*/" matches "a", "abc", "abcbc", "abcbcbc", ...
    "/a.*a/" matches "aa", "aba", "a8qa", "a!?_a", ...
    
    • means 1 or more occurrences

      "/a(bc)+/" matches "abc", "abcbc", "abcbcbc", ...
      "/Goo+gle/" matches "Google", "Gooogle", "Goooogle", ...
      
  • ? means 0 or 1 occurrences
"/a(bc)?/" matches "a" or "abc”
  • {min,max} means between min and max occurrences (inclusive)

    "/a(bc){2,4}/" matches "abcbc", "abcbcbc", or "abcbcbcbc"
    
  • min or max may be omitted to specify any number

    {2,} means 2 or more
    {,6} means up to 6
    {3} means exactly 3
    
  • [] group characters into a character set; will match any single character from the set

    "/[bcd]art/" matches strings containing "bart", "cart", and "dart"
    equivalent to "/(b|c|d)art/" but shorter
    
  • inside [], many of the modifier keys act as normal characters

    "/what[!*?]*/" matches "what", "what!", "what?**!", "what??!", ...
    
  • inside a character set, specify a range of characterswith - “/[a-z]/“ matches any lowercase letter
  • “/[a-zA-Z0-9]/“ matches any lower- or uppercase letter or digit
  • an initial ^ inside a character set negates it “/[^abcd]/“ matches any character other than a, b, c, or d
  • inside a character set, - must be escaped to be matched “/[+-]?[0-9]+/“ matches an optional + or -, followed by at least one digit
  • special escape sequence character sets:

    \d matches any digit (same as [0-9]); \D any non-digit ([^0-9])
    \w matches any word character (same as [a-zA-Z_0-9]); \W any nonword
    char
    \s matches any whitespace character ( \r, \t, \n, etc.); \S any nonwhitespace
    

实例

  1. at least $1000.00

    "/\$[1-9]\d{3,}\.\d{2}/"
    
  2. Email

    \w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
    
文章目录
  1. 1. 什么是正则表达式
  2. 2. 基本语法
  3. 3. 实例