当前位置:网站首页>regular expression

regular expression

2022-06-27 21:59:00 JAPAN_ is_ shit

Preface

Regular expression Basics

One . Character set

Some character sets are predefined in regular expressions , The character set can express some matching rules represented by meta characters and ordinary characters in a concise way .

The following is the character set , In the character set unicode Characters within , So in both Chinese and English [0-9] Representation number ,[A-Za-z] It means that the letters are accurate .

.    Matching elimination \n\r Any character of 
\d   Match a number , Approximately equivalent [0-9], Wider scope 
\D   Match a non number 
\w   Match an alphanumeric underlined word character , Approximately equivalent [A-Za-z0-9_], Wider scope 
\W   Match a non word character 
\s   Match an invisible character 
\S   Match a visible character 
\b   Match the boundaries of words , Matching position 
\B   Match the non boundary of the word 

Two . qualifiers

*    Match the previous expression any number of times 
+    Match the previous expression 1 Times or times 
?    Match the previous expression 0 Once or once 
()   Define the expression between as “ Group ”(group), They can use  \1  To \9  To quote .
	 (q)(w)\1\2  Will match to the qwqw
[]   Match any of the characters it contains 
{
    }   The matching times of the previous expression 
|    Logic or operation 
-    A hyphen , Scope of representation 
^    Start matching from the string header 
$    Match from the end of the string 

3、 ... and . Common expression

1. Matching position

(pattern)   Get matching pattern value 
 The following four are non fetch matches , Mainly used to locate 
(?=pattern)  Positive positive matching   123(?=\.jpg)  To match .jpg Previous 123
(?<=pattern)  Reverse positive match   (?<=123)\.jpg  To match 123 hinder .jpg
(?!pattern)  Positive negative matching   123(?!\.jpg)  Finger mismatch .jpg Previous 123
(?<!pattern)  Reverse negative match   (?<!123)\.jpg  Finger mismatch 123 hinder .jpg

2. Group references

([A-Za-z])(\1)    (\1) Get the value of the first group  , Match two consecutive identical letters 
([A-Za-z])([A-Za-z])(\1)(\2)  ABAB Type character 
([\u4e00-\u9fa5])(\1)([\u4e00-\u9fa5])(\3)  AABB Chinese Idioms 

3. Non greedy model

+ * {
    } The default is greedy mode , add ? It will be transformed into a non greedy mode 
原网站

版权声明
本文为[JAPAN_ is_ shit]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/178/202206271936377295.html