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:
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:
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:
- Represents a range of values, is also one of many cases, regular representation
[1-6a-f]
, the diagram is as follows:
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]
[-fa]
[a-f]
- Exclude partial values, that is, write out the unwanted values, re
[^abc]
Represents the exclusion of one of ABC, as shown below:
- Several common abbreviations
- \d = [0-9]
- \D = [^0-9]
- \w = [0-9a-zA-Z_]
- \W = [^0-9a-zA-Z]
- \s = [\t\v\n\r\f]
- \S = [^\t\v\n\r\f]
- . = [^\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
- {m} = {m,m} = m occurrences. For example, A {9} indicates that A appears 9 times, as shown in the figure:
- ? = {0,1} = none or once. Such as a? Indicates that A does not appear or appears once, as shown in the figure:
- + = {1,} = occurs at least once. For example, a+ means that A occurs at least once, as shown in the figure:
- * = {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:
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:
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!!