当前位置:网站首页>Regular expression introduction and some syntax

Regular expression introduction and some syntax

2022-06-26 00:16:00 Big dream_ Thousands of autumn

Simple translation of characters

Symbol meaning
\n Represents a newline character
\t For tab
\\ representative \ In itself
\^,\$,\(,\),\{,\},\?,\+,\*,|,\[,\] Match the symbols themselves

Standard character set

Can and Multiple characters Matching expression
Be case sensitive , Capital letters mean the opposite

Symbol meaning
\d Any number ,0~9 Any one of
\D The digital
\w Any alphanumeric or underscore ,A-Z,a-z,0~9,_
\W Any character except alphanumeric underscores
\s Including Spaces 、 tabs 、 Any one of white space characters such as line feed
\S Except for the space 、 tabs 、 Any character other than the newline character
. The decimal point can match any character , But it doesn't match the newline character , If you want to match, include \n All the characters in , It's usually used [\s\S]

Custom character set

[] Square brackets match : Can match any character in the formula bracket

expression Matching character
[[email protected]] matching a perhaps b perhaps @ perhaps 5
^ab] Match except a,b Any character other than
[f-k] matching f To k Any character between , Include f and k
[^A-F0-3] Match except A ~ F,0 ~ 3 Any character other than
  • stay [ ] in ^ It means to take an objection .
  • Special symbols for regular expressions , Enclosed in square brackets , Is to go to the special significance , except ^ and - outside .
  • Standard character set , Except that the decimal point in brackets will lose its special meaning , Others don't . [\dab], Match numbers or a,b

quantifiers

A special symbol that modifies the number of matches

expression meaning
{n} The expression repeats n Time , Include n
{m,n} The expression repeats at least m Time , Repeat at most n Time , Include m and n
{m,} The expression repeats at least m Time , Include m
? Match expression 0 Time or 1 Time , amount to {0,1}
+ The expression appears at least once , amount to {1,}
* The expression does not appear or occurs any time , amount to {0,}
  • Greedy mode : The more characters you match, the better , Default . For example, if the following figure is written as c{2,6}, It will match one cccccc.
  • Non greedy model : Is to add a after the quantifier ? Number . Or the picture below , It's written in c{2,6}?, It will match to 3 individual cc

If it's continuous 6 individual c, Use expressions c{2,3}, Will match to the 2 individual ccc, This is because it matches the first one ccc Time will Recount .
 Insert picture description here

Character boundary

Symbol meaning
^ What to start with
$ To what end
\b The left and right boundaries are not all \w , That is, there are not all letters on the left and right , Numbers , Underline

Regular matching patterns

  • Ignore case mode
  • One way mode : The entire text is treated as a string , There is only one beginning , An end .
  • Multi line mode : Each line is a string , There's a beginning and an end .

Selector or grouping

expression effect
| Between left and right expressions or Relationship , Match left , Or on the right
() Capture group 1. At the time of being decorated with matching times , Expressions in parentheses can be decorated as a whole .2. Time to get the matching result , The expression in parentheses matches The content can be obtained separately

backreferences : Each pair of () A number will be assigned , Use () Capture of from... According to the left parenthesis 1 Start AutoNumber .
By reverse reference , You can reference strings that have been captured by grouping , Here's the picture : The expression in parentheses means that two letters are matched , hinder \1 Said to () References to the contents of , namely () Match to go,\1 Quote again go, At this time, what this regularization needs is gogo
 Insert picture description here

Assertion

  • Match only subexpressions , The matching content is not included in the matching result
  • This position should meet certain conditions
expression meaning
(?=exp) Assert where it appears The back can be Match to expression exp
(?<=exp) Assert where it appears Front can Match to expression exp
(?!exp) Assert where it appears Not in the back Match to expression exp
(?<!exp) Assert where it appears Not in front Match to expression exp

for example : Assert that you can match with ing The characters of
 Insert picture description here

原网站

版权声明
本文为[Big dream_ Thousands of autumn]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206252124220610.html