Regular expression memo (recommended collection)

Posted Jun 29, 20202 min read

Anchor

  • ^:start of string or start of line in multi-line mode
  • $:end of string or end of line in multi-line mode
  • \b:word boundary
  • \B:not a word boundary(relative to \b)

Note:The anchor point is not quantifiable(ie, there can be no quantifier behind it).

Character sequence

  • .:any character except newline
  • \d:any numeric character
  • \D:any non-numeric character(relative to \d)
  • \w: any text character
  • \W:any non-word character(relative to \w)
  • \s:any blank character
  • \S:any non-whitespace character(relative to \s)
  • [abc]:given a character(here a, b or c)
  • [^abc]:a single character that is not in the given set(relative to [abc])
  • [a-z]:a single character within a given range(between a and z)
  • [^a-z]:a single character that is not in the given range(relative to [a-z])
  • [a-zA-Z]:a single character within a given range

Note:Use \ to escape special characters(such as \, /, [, ], (, ), {, }, etc.).

quantifier

  • a?:zero or one a(equal to a{0,1})
  • a*:zero or more a(equal to a{0,})
  • a+:(a is equal to one or more of a{1,})
  • a{3}:exactly 3 a
  • a{3,}:3 or more a
  • a{3,5}:between 3 and 5 a(inclusive)

Note:a is any valid quantifiable expression.

group

  • (ab):match and capture all included content
  • (a|b):match and capture a character
  • (?:ab):Match all closed content, do not capture

Sign

  • g:global
  • m:multiple lines
  • i:not case sensitive
  • u:Unicode

Note that this article is only a memo, an introduction, not a complete guide to regular expression features and nuances. In the next article, we introduce the practical solution of regular expressions in the project.


THE END

Thanks for reading

The public number is the starting platform, pay attention to not get lost, find the organization and make progress together