Introduction to the

Regular expressions are inescapable. The power of regular expressions is well known, and learning it is also of great help to our programming

Fuzzy matching

Regular expressions are powerful because of their fuzzy matching. They cannot match different numbers of strings, but they can also match different contents of strings. If you can only match strings exactly, then the re effect is useless. For example, /a/ matches an A character.

A row match that matches the length of a string

The length of the matching string is not fixed, as shown in the figure below:

image-20200910170307203

Vertical matching used to match the contents of a string in multiple cases

The content of the matched string is one of several cases, as shown:

image-20200910170403956

Character groups

When we write a re match, there are many possible values in one position, such as an index of a string, let’s say one of the letters ABC. So we need to use a character set, which basically means one of a set of characters.

  • Represents a number of possible situations at a location, represented by a regular/[abc]/, the diagram is as follows:
image-20200910170841308
  • Represents a range of values, is also one of many cases, regular representation[1-6a-f], the diagram is as follows:
image-20200910171154129

Note that sometimes all we need to match are the three values that represent the range, for example, a-f. Instead of writing our re [a-f], we need to write it like this:

[-af]

img

[-fa]

img

[a-f]


  • Exclude partial values, that is, write out the unwanted values, re[^abc]Represents the exclusion of one of ABC, as shown below:
image-20200910172039436
  • Several common abbreviations
  1. \d = [0-9]

img
  1. \D = [^0-9]
img
img
  1. \w = [0-9a-zA-Z_]
img
img
  1. \W = [^0-9a-zA-Z]
img
img
  1. \s = [\t\v\n\r\f]
img
img
  1. \S = [^\t\v\n\r\f]

img
  1. . = [^\n\r\u2028\u2029]


quantifiers

shorthand

  • {m,} occurs at least m times, where m is a quantity. For example, a{9,} indicates that A appears at least 9 times, as shown in the figure
img
  • {m} = {m,m} = m occurrences. For example, A {9} indicates that A appears 9 times, as shown in the figure:
img
  • ? = {0,1} = none or once. Such as a? Indicates that A does not appear or appears once, as shown in the figure:
img
  • + = {1,} = occurs at least once. For example, a+ means that A occurs at least once, as shown in the figure:
img
  • * = {0,}= occur any number of times. For example, a* indicates that A appears any number of times, maybe many times, or not at all, as shown in the figure:
img

Multiple branch

Also is a kind of with | said many kinds of situations, such as: a | b | c, said one of ABC, as shown in figure:

img

summary

The above is to learn the preliminary understanding of the regular and the diagram, with the graph to represent the regular is very good to understand, welcome everyone to look up, there is a problem please point out, feel good, can give a thumb-up, thank you!!