æ£åè¡¨è¾¾å¼ (regex) çå¿«éåèï¼å æ¬ç¬¦å·ãèå´ãåç»ãæè¨åä¸äºç¤ºä¾æ¨¡å¼ï¼ä»¥å¸®å©æ¨å ¥é¨ã
è¿æ¯å¼å§ä½¿ç¨æ£å表达å¼(Regex)çå¿«éå¤å¿åã
| èä¾ | 说æ |
|---|---|
[abc] | å个å符ï¼aãb æ c |
[^abc] | ä¸ä¸ªå符ï¼é¤äºï¼aãb æ c |
[a-z] | èå´å
çå符ï¼a-z |
[^a-z] | ä¸å¨èå´å
çå符ï¼a-z |
[0-9] | èå´å
çæ°åï¼0-9 |
[a-zA-Z] | èå´å
çå符ï¼a-z æ A-Z |
[a-zA-Z0-9] | èå´å
çå符ï¼a-zãA-Z æ 0-9 |
| èä¾ | 说æ |
|---|---|
a? | é¶ä¸ªæä¸ä¸ªa |
a* | é¶ä¸ªæå¤ä¸ª a |
a+ | ä¸ä¸ªæå¤ä¸ªa |
[0-9]+ | 0-9ä¸çä¸ä¸ªæå¤ä¸ª |
a{3} | æ£å¥½ 3 个 a |
a{3,} | 3ä¸ªææ´å¤ça |
a{3,6} | a ç 3 å° 6 ä¹é´ |
a* | è´ªå¿éè¯ |
a*? | æ°æ§éè¯ |
a*+ | å æéè¯ |
| æ¨¡å¼ | æè¿° |
|---|---|
^ | å¹é å符串çå¼å¤´ |
{ | å¼å§ä¸ä¸ªæ°ééå®ç¬¦ï¼æå®åºç°æ¬¡æ° |
+ | å¹é åé¢çå ç´ ä¸æ¬¡æå¤æ¬¡ |
< | éæ åçæ£å表达å¼å å符ï¼å¨ HTML ä¸å¸¸ç¨ï¼ |
[ | å¼å§ä¸ä¸ªå符类 |
* | å¹é åé¢çå ç´ é¶æ¬¡æå¤æ¬¡ |
) | ç»ææè·ç» |
> | éæ åçæ£å表达å¼å å符ï¼å¨ HTML ä¸å¸¸ç¨ï¼ |
. | å¹é 餿¢è¡ç¬¦ä¹å¤çä»»æå符 |
( | å¼å§ä¸ä¸ªæè·ç» |
| | 卿£åè¡¨è¾¾å¼æ¨¡å¼ä¸ä½ä¸ºé»è¾ææä½ |
$ | å¹é å符串çç»å°¾ |
\ | 转ä¹å å符ï¼ä½¿å ¶å ·æåé¢æä¹ |
? | å¹é åé¢çå ç´ é¶æ¬¡æä¸æ¬¡ |
ä½¿ç¨ \ 转ä¹è¿äºç¹æ®å符
| èä¾ | 说æ |
|---|---|
. | ä»»ä½å个å符 |
\s | ä»»ä½ç©ºç½å符 |
\S | ä»»ä½é空ç½å符 |
\d | 任使°åï¼ä¸ [0-9] ç¸å |
\D | ä»»ä½éæ°åï¼ä¸ [^0-9] ç¸å |
\w | ä»»ä½åè¯å符 |
\W | ä»»ä½éåè¯å符 |
\X | ä»»ä½ Unicode åºåï¼å æ¬æ¢è¡ç¬¦ |
\C | å¹é ä¸ä¸ªæ°æ®åå |
\R | Unicode æ¢è¡ç¬¦ |
\v | åç´ç©ºç½å符 |
\V | \v çå¦å® - é¤äºæ¢è¡ç¬¦ååç´å¶è¡¨ç¬¦ä¹å¤çä»»ä½å
容 |
\h | 水平空ç½å符 |
\H | \h çå¦å® |
\K | éç½®å¹é |
\n | å¹é
第 n ä¸ªåæ¨¡å¼ |
\pX | Unicode 屿§ X |
\p{...} | Unicode 屿§æèæ¬ç±»å« |
\PX | \pX çå¦å® |
\P{...} | \p çå¦å® |
\Q...\E | å¼ç¨;è§ä¸ºæå |
\k<name> | å¹é
忍¡å¼name |
\k'name' | å¹é
忍¡å¼name |
\k{name} | å¹é
忍¡å¼name |
\gn | å¹é 第 n ä¸ªåæ¨¡å¼ |
\g{n} | å¹é 第 n ä¸ªåæ¨¡å¼ |
\g<n> | éå½ç¬¬ n 个æè·ç» |
\g'n' | éå½ç¬¬ n 个æè·ç»ã |
\g{-n} | å¹é 第 n 个ç¸å¯¹åä¸ä¸ªåæ¨¡å¼ |
\g<+n> | éå½ç¬¬ n 个ç¸å¯¹å³å°å°æ¥ç忍¡å¼ |
\g'+n' | å¹é 第 n 个ç¸å¯¹å³å°å°æ¥ç忍¡å¼ |
\g'letter' | éå½å½åæè·ç» 忝 |
\g{letter} | å¹é
å
åå½åçæè·ç» 忝 |
\g<letter> | éå½å½åæè·ç» 忝 |
\xYY | åå
è¿å¶å符 YY |
\x{YYYY} | åå
è¿å¶å符 YYYY |
\ddd | å
«è¿å¶å符ddd |
\cY | æ§å¶å符 Y |
[\b] | éæ ¼å符 |
\ | 使任ä½å符æå |
| èä¾ | 说æ |
|---|---|
\G | æ¯èµå¼å§ |
^ | å符串çå¼å§ |
$ | åç¬¦ä¸²ç»æ |
\A | å符串çå¼å§ |
\Z | åç¬¦ä¸²ç»æ |
\z | å符串çç»å¯¹ç»å°¾ |
\b | ä¸ä¸ªè¯çè¾¹ç |
\B | éåè¯è¾¹ç |
| èä¾ | 说æ |
|---|---|
\0 | 宿´çæ¯èµå 容 |
\1 | æè·ç» 1 ä¸çå
容 |
$1 | æè·ç» 1 ä¸çå
容 |
${foo} | æè·ç» foo ä¸çå
容 |
\x20 | åå è¿å¶æ¿æ¢å¼ |
\x{06fa} | åå è¿å¶æ¿æ¢å¼ |
\t | æ ç¾ |
\r | å车 |
\n | æ°é |
\f | æ¢é¡µ |
\U | 大åè½¬æ¢ |
\L | å°åè½¬æ¢ |
\E | ç»æ¢ä»»ä½è½¬æ¢ |
| èä¾ | 说æ |
|---|---|
(...) | æè·ææå°éçä¸è¥¿ |
(a|b) | å¹é
a æ b |
(?:...) | å¹é ééçææå 容 |
(?>...) | ååç»ï¼éæè·ï¼ |
(?|...) | éå¤ç忍¡å¼ç»å· |
(?#...) | 注解 |
(?'name'...) | å½åæè·ç» |
(?<name>...) | å½åæè·ç» |
(?P<name>...) | å½åæè·ç» |
(?imsxXU) | å è修饰符 |
(?(DEFINE)...) | å¨ä½¿ç¨å®ä»¬ä¹åé¢å®ä¹æ¨¡å¼ |
| :- | - |
|---|---|
(?(1)yes|no) | æ¡ä»¶è¯å¥ |
(?(R)yes|no) | æ¡ä»¶è¯å¥ |
(?(R#)yes|no) | é彿¡ä»¶è¯å¥ |
(?(R&name)yes|no) | æ¡ä»¶è¯å¥ |
(?(?=...)yes|no) | ææ¡ä»¶çåç» |
(?(?<=...)yes|no) | ææ¡ä»¶çå¾åç |
| :- | - |
|---|---|
(?R) | é彿´ä¸ªæ¨¡å¼ |
(?1) | éå½ç¬¬ä¸ä¸ªåæ¨¡å¼ |
(?+1) | éå½ç¬¬ä¸ä¸ªç¸å¯¹åæ¨¡å¼ |
(?&name) | éå½å模å¼name |
(?P=name) | å¹é
忍¡å¼name |
(?P>name) | éå½å模å¼name |
| :- | - |
|---|---|
g | å ¨é¨ |
m | å¤è¡ |
i | ä¸åºå大å°å |
x | 忽ç¥ç©ºæ ¼ |
s | å线 |
u | ç»ä¸ç |
X | æ©å± |
U | ä¸è´ªå¿ |
A | é |
J | éå¤çç»å |
d | ç»æå 嫿è·ç»åå符串å¼å§åç»æçç´¢å¼ |
| :- | - |
|---|---|
(?=...) | æ£å è¡æè¨ |
(?!...) | è´å è¡æè¨ |
(?<=...) | æ£ååæè¨ |
(?<!...) | è´ååæè¨ |
?= | æ£å è¡æè¨-åå¨ |
?! | è´å è¡æè¨-æé¤ |
?<= | æ£ååæè¨-åå¨ |
?<! | è´ååæè¨-æé¤ |
é¶å®½åº¦æè¨ å 许æ¨å¨ä¸»æ¨¡å¼ä¹åï¼ååçï¼æä¹åï¼lookaheadï¼å¹é ä¸ä¸ªç»ï¼èä¸ä¼å°å ¶å å«å¨ç»æä¸ã
| å符类 | å¦å | æä¹ |
|---|---|---|
[[:alnum:]] | [0-9A-Za-z] | 忝忰å |
[[:alpha:]] | [A-Za-z] | 忝 |
[[:ascii:]] | [\x00-\x7F] | ASCII ç 0-127 |
[[:blank:]] | [\t ] | ä» ç©ºæ ¼æå¶è¡¨ç¬¦ |
[[:cntrl:]] | [\x00-\x1F\x7F] | æ§å¶å符 |
[[:digit:]] | [0-9] | åè¿å¶æ°å |
[[:graph:]] | [[:alnum:][:punct:]] | å¯è§å符ï¼ä¸æ¯ç©ºæ ¼ï¼ |
[[:lower:]] | [a-z] | å°å忝 |
[[:print:]] | [ -~] == [ [:graph:]] | å¯è§å符 |
[[:punct:]] | [!"#$%&â()*+,-./:;<=>?@[]^_`{|}~] | å¯è§æ ç¹ç¬¦å· |
[[:space:]] | [\t\n\v\f\r ] | ç©ºç½ |
[[:upper:]] | [A-Z] | 大å忝 |
[[:word:]] | [0-9A-Za-z_] | åè¯å符 |
[[:xdigit:]] | [0-9A-Fa-f] | åå è¿å¶æ°å |
[[:<:]] | [\b(?=\w)] | è¯çå¼å¤´ |
[[:>:]] | [\b(?<=\w)] | è¯å°¾ |
| :- | - |
|---|---|
(*ACCEPT) | æ§å¶å¨è¯ |
(*FAIL) | æ§å¶å¨è¯ |
(*MARK:NAME) | æ§å¶å¨è¯ |
(*COMMIT) | æ§å¶å¨è¯ |
(*PRUNE) | æ§å¶å¨è¯ |
(*SKIP) | æ§å¶å¨è¯ |
(*THEN) | æ§å¶å¨è¯ |
(*UTF) | 徿¡ä¿®é¥°ç¬¦ |
(*UTF8) | 徿¡ä¿®é¥°ç¬¦ |
(*UTF16) | 徿¡ä¿®é¥°ç¬¦ |
(*UTF32) | 徿¡ä¿®é¥°ç¬¦ |
(*UCP) | 徿¡ä¿®é¥°ç¬¦ |
(*CR) | æ¢è¡ä¿®é¥°ç¬¦ |
(*LF) | æ¢è¡ä¿®é¥°ç¬¦ |
(*CRLF) | æ¢è¡ä¿®é¥°ç¬¦ |
(*ANYCRLF) | æ¢è¡ä¿®é¥°ç¬¦ |
(*ANY) | æ¢è¡ä¿®é¥°ç¬¦ |
\R | æ¢è¡ä¿®é¥°ç¬¦ |
(*BSR_ANYCRLF) | æ¢è¡ä¿®é¥°ç¬¦ |
(*BSR_UNICODE) | æ¢è¡ä¿®é¥°ç¬¦ |
(*LIMIT_MATCH=x) | æ£å表达å¼å¼æä¿®é¥°ç¬¦ |
(*LIMIT_RECURSION=d) | æ£å表达å¼å¼æä¿®é¥°ç¬¦ |
(*NO_AUTO_POSSESS) | æ£å表达å¼å¼æä¿®é¥°ç¬¦ |
(*NO_START_OPT) | æ£å表达å¼å¼æä¿®é¥°ç¬¦ |
| èä¾ | 说æ |
|---|---|
ring | å¹é
|
. | å¹é
|
h.o | å¹é
|
ring\? | å¹é
|
\(quiet\) | å¹é
|
c:\\windows | å¹é
|
ä½¿ç¨ \ æç´¢è¿äºç¹æ®å符ï¼
[ \ ^ $ . | ? * + ( ) { }
| èä¾ | 说æ |
|---|---|
\w | âåè¯âå符 (åæ¯ãæ°åæä¸å线) |
\d | æ°å |
\s | ç©ºæ ¼ (ç©ºæ ¼ãå¶è¡¨ç¬¦ãvtabãæ¢è¡ç¬¦) |
\W, \D, or \S | 䏿¯åè¯ãæ°åæç©ºæ ¼ |
[\D\S] | è¡¨ç¤ºä¸æ¯æ°åæç©ºæ ¼ï¼ä¸¤è é½å¹é |
[^\d\s] | ç¦æ¢æ°ååç©ºæ ¼ |
| èä¾ | 说æ |
|---|---|
colou?r | å¹é
|
[BW]ill[ieamy's]* | å¹é
|
[a-zA-Z]+ | å¹é 1 个æå¤ä¸ªåæ¯ |
\d{3}-\d{2}-\d{4} | å¹é SSN |
[a-z]\w{1,7} | å¹é UW NetID |
| èä¾ | 说æ |
|---|---|
cat|dog | å¹é
|
id|identity | å¹é
|
identity|id | å¹é
|
彿¿ä»£åéå æ¶ï¼å½ä»¤ä»é¿å°ç
| èä¾ | 说æ |
|---|---|
[aeiou] | å¹é ä»»ä½å é³ |
[^aeiou] | å¹é ä¸ä¸ªéå é³ |
r[iau]ng | å¹é
|
gr[ae]y | å¹é
|
[a-zA-Z0-9] | å¹é ä»»ä½åæ¯ææ°å |
å¨ [ ] 䏿»æ¯è½¬ä¹ . \ ] ææ¶æ¯ ^ - .
| èä¾ | 说æ |
|---|---|
* + {n,}greedy | å°½å¯è½å¹é |
<.+> | å¨ |
*? +? {n,}?lazy | å°½å¯è½å°å¹é |
<.+?> | å¨ < |
| èä¾ | 说æ |
|---|---|
\b | âåè¯âè¾¹ç¼ï¼éâåè¯âå符æè¾¹ï¼ |
\bring | åè¯ä»¥âringâå¼å¤´ï¼ä¾å¦ |
ring\b | åè¯ä»¥âringâç»å°¾ï¼ä¾å¦ |
\b9\b | å¹é
å个æ°å |
\b[a-zA-Z]{6}\b | å¹é 6 ä¸ªåæ¯çåè¯ |
\B | 䏿¯åè¾¹ |
\Bring\B | å¹é
|
^\d*$ | æ´ä¸ªåç¬¦ä¸²å¿ é¡»æ¯æ°å |
^[a-zA-Z]{4,20}$ | åç¬¦ä¸²å¿ é¡»æ 4-20 ä¸ªåæ¯ |
^[A-Z] | åç¬¦ä¸²å¿ é¡»ä»¥å¤§å忝å¼å¤´ |
[\.!?"')]$ | åç¬¦ä¸²å¿ é¡»ä»¥ç»ç«¯æ ç¹ç»å°¾ |
| èä¾ | 说æ |
|---|---|
(?i)[a-z]*(?-i) | 忽ç¥å¤§å°åå¼/å ³ |
(?s).*(?-s) | å¹é å¤è¡ï¼å¯¼è´ . å¹é æ¢è¡ç¬¦ï¼ |
(?m)^.*;$(?-m) | |
(?x) | #free-spacing 模å¼ï¼æ¤ EOL 注éè¢«å¿½ç¥ |
(?-x) | èªç±ç©ºé´æ¨¡å¼å ³é |
/regex/ismx | ä¿®æ¹æ´ä¸ªåç¬¦ä¸²çæ¨¡å¼ |
| èä¾ | 说æ |
|---|---|
(in|out)put | å¹é
|
\d{5}(-\d{4})? | ç¾å½é®æ¿ç¼ç (â+ 4âå¯é) |
妿ç»åå¹é
失败ï¼è§£æå¨ä¼å°è¯æ¯ä¸ªæ¿ä»£æ¹æ¡ã
å¯è½å¯¼è´ç¾é¾æ§çåæº¯ã
| èä¾ | 说æ |
|---|---|
(to) (be) or not \1 \2 | å¹é
|
([^\s])\1{2} | å¹é
éç©ºæ ¼ï¼ç¶ååç¸å两次  |
\b(\w+)\s+\1\b | å¹é åå |
| èä¾ | 说æ |
|---|---|
on(?:click|load) | å¿«äºï¼on(click|load) |
å°½å¯è½ä½¿ç¨éæè·æååç»
| èä¾ | 说æ |
|---|---|
(?>red|green|blue) | æ¯éæè·æ´å¿« |
(?>id|identity)\b | å¹é
|
\b å¨ååç»ä¹å失败ï¼
è§£æå¨ä¸ä¼å溯å°ç»ä»¥éè¯â身份â
妿æ¿ä»£åéå ï¼è¯·ä»é¿å°çå½ä»¤ã
| èä¾ | 说æ |
|---|---|
(?= ) | ååçï¼å¦æä½ è½æåæ¾å° |
(?! ) | ååçï¼å¦æä½ æ¾ä¸å°åé¢ |
(?<= ) | ååçï¼å¦æä½ è½æ¾å°åé¢ |
(?<! ) | ååçï¼å¦æä½ æ¾ä¸å°åé¢ |
\b\w+?(?=ing\b) | å¹é
|
\b(?!\w+ing\b)\w+\b | ä¸ä»¥âingâç»å°¾çåè¯ |
(?<=\bpre).*?\b | å¹é
pre |
\b\w{3}(?<!pre)\w*?\b | ä¸ä»¥âpreâå¼å¤´çè¯ |
\b\w+(?<!ing)\b | å¹é ä¸ä»¥âingâç»å°¾çåè¯ |
å¹é
Mr. æ Ms. 妿åè¯ her ç¨åå¨å符串ä¸
M(?(?=.*?\bher\b)s|r)\.
éè¦ç¯é¡¾ IF æ¡ä»¶
| è¡¨è¾¾å¼ | å¹é ç¤ºä¾ |
|---|---|
the | The fat cat sat on the mat. |
The | The fat cat sat on the mat. |
ç±åæ¯tå¼å§ï¼æ¥çæ¯hï¼åæ¥çæ¯e
.| è¡¨è¾¾å¼ | å¹é ç¤ºä¾ |
|---|---|
.ar | The car parked in the garage. |
表达å¼.arå¹é
ä¸ä¸ªä»»æå符åé¢è·çæ¯aårçå符串
| è¡¨è¾¾å¼ | å¹é ç¤ºä¾ |
|---|---|
.ar | The car pargarage. |
ar[.] | A garage is a good place to park a car. |
æ¹æ¬å·çå¥å·å°±è¡¨ç¤ºå¥å·ãè¡¨è¾¾å¼ ar[.] å¹é
ar. å符串
| è¡¨è¾¾å¼ | å¹é ç¤ºä¾ |
|---|---|
[^c]ar | The car parked in the garage. |
è¡¨è¾¾å¼ [^c]ar å¹é
ä¸ä¸ªåé¢è·ç ar çé¤äºcçä»»æå符ã
* å·| è¡¨è¾¾å¼ | å¹é ç¤ºä¾ |
|---|---|
[a-z]* | The carparked inthe garage |
\s*cat\s* | The fat cat sat on the concatenation. |
è¡¨è¾¾å¼ [a-z]* å¹é
ä¸ä¸ªè¡ä¸ææä»¥å°å忝å¼å¤´çå符串ã
+ å·| è¡¨è¾¾å¼ | å¹é ç¤ºä¾ |
|---|---|
c.+t | The fat cat sat on the mat. |
è¡¨è¾¾å¼ c.+t å¹é
以é¦åæ¯cå¼å¤´ä»¥tç»å°¾ï¼ä¸é´è·çè³å°ä¸ä¸ªå符çå符串ã
? å·| è¡¨è¾¾å¼ | å¹é ç¤ºä¾ |
|---|---|
[T]he | The car is parked in the garage. |
[T]?he | The car is parked in the garage. |
è¡¨è¾¾å¼ [T]?he å¹é
å符串 he å Theã
{} å·| è¡¨è¾¾å¼ | å¹é ç¤ºä¾ |
|---|---|
[0-9]{2,3} | The number was 9.9997 but we rounded it off to 10.0. |
[0-9]{2,} | The number was 9.9997 but we rounded it off to 10.0. |
[0-9]{3} | The number was 9.9997 but we rounded it off to 10.0. |
(...) ç¹å¾æ 群| è¡¨è¾¾å¼ | å¹é ç¤ºä¾ |
|---|---|
(c|g|p)ar | The car is parked in the garage. |
è¡¨è¾¾å¼ (c|g|p)ar å¹é
car æ gar æ parã æ³¨æ \ æ¯å¨ Markdown ä¸ä¸ºäºä¸ç ´åè¡¨æ ¼è½¬ä¹ |ã
| æè¿ç®ç¬¦| è¡¨è¾¾å¼ | å¹é ç¤ºä¾ |
|---|---|
(T|t)he|car | The car is parked in the garage. |
è¡¨è¾¾å¼ (T|t)he|car å¹é
(T|t)he æ car
| è¡¨è¾¾å¼ | å¹é ç¤ºä¾ |
|---|---|
(f|c|m)at\.? | The fat cat sat on the mat. |
妿æ³è¦å¹é
å¥åä¸ç . åè¦åæ \. 以ä¸è¿ä¸ªä¾å \.? æ¯éæ©æ§å¹é
.
å¹é æå®å¼å¤´æç»å°¾çå符串就è¦ä½¿ç¨å°éç¹ã
^ å· (符串çå¼å¤´)| è¡¨è¾¾å¼ | å¹é ç¤ºä¾ |
|---|---|
(T|t)he | The car is parked in the garage. |
^(T|t)he | The car is parked in the garage. |
$ å· (妿¯æåä¸ä¸ª)| è¡¨è¾¾å¼ | å¹é ç¤ºä¾ |
|---|---|
(at\.) | The fat cat. sat. on the mat. |
(at\.)$ | The fat cat. sat. on the mat. |
| ç®å | æè¿° |
|---|---|
. | 餿¢è¡ç¬¦å¤çææå符 |
\w | å¹é
ææåæ¯æ°å çåäº [a-zA-Z0-9_] |
\W | å¹é
ææé忝æ°åï¼å³ç¬¦å· çåäºï¼ [^\w] |
\d | å¹é
æ°åï¼ [0-9] |
\D | å¹é
éæ°åï¼ [^\d] |
\s | å¹é
ææç©ºæ ¼å符 çåäºï¼ [\t\n\f\r\p{Z}] |
\S | å¹é
ææéç©ºæ ¼åç¬¦ï¼ [^\s] |
\f | å¹é ä¸ä¸ªæ¢é¡µç¬¦ |
\n | å¹é ä¸ä¸ªæ¢è¡ç¬¦ |
\r | å¹é ä¸ä¸ªå车符 |
\t | å¹é ä¸ä¸ªå¶è¡¨ç¬¦ |
\v | å¹é ä¸ä¸ªåç´å¶è¡¨ç¬¦ |
\p | å¹é
CR/LF(çåäº \r\n)ç¨æ¥å¹é DOS è¡ç»æ¢ç¬¦ |
æ£åè¡¨è¾¾å¼æä¾ä¸äºå¸¸ç¨çå符éç®åã
?=... æ£å
è¡æè¨| è¡¨è¾¾å¼ | å¹é ç¤ºä¾ |
|---|---|
(T|t)he(?=\sfat) | The fat cat sat on the mat. |
The å the åé¢ç´§è·ç (ç©ºæ ¼)fatã
?!... è´å
è¡æè¨| è¡¨è¾¾å¼ | å¹é ç¤ºä¾ |
|---|---|
(T|t)he(?!\sfat) | The fat cat sat on the mat. |
å¹é
The å theï¼ä¸å
¶åä¸è·ç (ç©ºæ ¼)fatã
?<= ... æ£ååæè¨| è¡¨è¾¾å¼ | å¹é ç¤ºä¾ |
|---|---|
(?<=(T|t)he\s)(fat|mat) | The fat cat sat on the mat. |
å¹é
fat å matï¼ä¸å
¶åè·ç The æ theã
?<!... è´ååæè¨| è¡¨è¾¾å¼ | å¹é ç¤ºä¾ |
|---|---|
(?<!(T|t)he\s)(cat) | The cat sat on cat. |
å¹é
catï¼ä¸å
¶åä¸è·ç The æ theã
| è¡¨è¾¾å¼ | å¹é ç¤ºä¾ |
|---|---|
The | The fat cat sat on the mat. |
/The/gi | The fat cat sat on the mat. |
ä¿®é¥°è¯ i ç¨äºå¿½ç¥å¤§å°åï¼g 表示å
¨å±æç´¢ã
| è¡¨è¾¾å¼ | å¹é ç¤ºä¾ |
|---|---|
/.(at)/ | The fat cat sat on the mat. |
/.(at)/g | The fat cat sat on the mat. |
è¡¨è¾¾å¼ /.(at)/g 表示æç´¢ ä»»æå符ï¼é¤äºæ¢è¡ï¼+ atï¼å¹¶è¿åå
¨é¨ç»æã
| è¡¨è¾¾å¼ | å¹é ç¤ºä¾ |
|---|---|
/.at(.)?$/ | The fat cat sat on the mat. |
/.at(.)?$/gm | The fatcat saton the mat. |
| è¡¨è¾¾å¼ | å¹é ç¤ºä¾ |
|---|---|
/(.*at)/ | The fat cat sat on the mat. |
/(.*?at)/ | The fat cat sat on the mat. |
å¯¼å ¥æ£åè¡¨è¾¾å¼æ¨¡å
import re
>>> sentence = 'This is a sample string'
>>> bool(re.search(r'this', sentence, flags=re.I))
True
>>> bool(re.search(r'xyz', sentence))
False
>>> re.findall(r'\bs?pare?\b', 'par spar apparent spare part pare')
['par', 'spar', 'spare', 'pare']
>>> re.findall(r'\b0*[1-9]\d{2,}\b', '0501 035 154 12 26 98234')
['0501', '154', '98234']
>>> m_iter = re.finditer(r'[0-9]+', '45 349 651 593 4 204')
>>> [m[0] for m in m_iter if int(m[0]) < 350]
['45', '349', '4', '204']
>>> re.split(r'\d+', 'Sample123string42with777numbers')
['Sample', 'string', 'with', 'numbers']
>>> ip_lines = "catapults\nconcatenate\ncat"
>>> print(re.sub(r'^', r'* ', ip_lines, flags=re.M))
* catapults
* concatenate
* cat
>>> pet = re.compile(r'dog')
>>> type(pet)
<class '_sre.SRE_Pattern'>
>>> bool(pet.search('They bought a dog'))
True
>>> bool(pet.search('A cat crossed their path'))
False
| 彿° | 说æ |
|---|---|
re.findall | è¿åå 嫿æå¹é 项çå表 |
re.finditer | è¿åä¸ä¸ªå¯è¿ä»£çå¹é
对象 (æ¯ä¸ªå¹é ä¸ä¸ª) |
re.search | 妿å符串ä¸çä»»ä½ä½ç½®åå¨å¹é 项ï¼åè¿å Match 对象 |
re.split | è¿åä¸ä¸ªå表ï¼å ¶ä¸åç¬¦ä¸²å¨æ¯æ¬¡å¹é æ¶è¢«æå |
re.sub | ç¨åç¬¦ä¸²æ¿æ¢ä¸ä¸ªæå¤ä¸ªå¹é 项 |
re.compile | ç¼è¯æ£åè¡¨è¾¾å¼æ¨¡å¼ä¾ä»¥åä½¿ç¨ |
re.escape | è¿åææé忝æ°ååææ çå符串 |
| :- | - | - |
|---|---|---|
re.I | re.IGNORECASE | 忽ç¥å¤§å°å |
re.M | re.MULTILINE | å¤è¡ |
re.L | re.LOCALE | 使 \wã\bã\s locale ä¾èµ |
re.S | re.DOTALL | ç¹å¹é ææ ï¼å æ¬æ¢è¡ç¬¦ï¼ |
re.U | re.UNICODE | 使 \wã\bã\dã\s unicode ä¾èµ |
re.X | re.VERBOSE | å¯è¯»é£æ ¼ |
| :- | :- |
|---|---|
dotAll | æ¯å¦ä½¿ç¨äº s 修饰符 |
flags | è¿åæ å¿çå符串 |
global | æ¯å¦ä½¿ç¨äº g (å
¨é¨)修饰符 |
hasIndices | æ¯å¦ä½¿ç¨äº d 修饰符 |
ignoreCase | å¹é
ææ¬çæ¶åæ¯å¦å¿½ç¥å¤§å°å i |
multiline | æ¯å¦è¿è¡å¤è¡æç´¢ m |
lastIndex | 该索å¼è¡¨ç¤ºä»åªéå¼å§ä¸ä¸ä¸ªå¹é |
source | æ£å表达å¼çææ¬ |
sticky | æç´¢æ¯å¦æ¯ sticky |
unicode | Unicode åè½æ¯å¦å¼å¯ |
| :- | :- |
|---|---|
match() | è·åå¹é ç»æ |
matchAll() | ææå¹é 项 |
replace() | æ¿æ¢ææç¬¦åæ£å模å¼çå¹é 项 |
search() | æç´¢ä»¥åå¾å¹é æ£å模å¼ç项 |
split() | åå²å符串è¿åå符串æ°ç» |
compile() | ï¼éæ°ï¼ç¼è¯æ£åè¡¨è¾¾å¼ |
exec() | æå®åç¬¦ä¸²ä¸æ§è¡ä¸ä¸ªæç´¢å¹é |
test() | æ£å表达å¼ä¸æå®çå符串æ¯å¦å¹é |
toString() | è¿å该æ£å表达å¼çå符串 |
let textA = 'I like APPles very much';
let textB = 'I like APPles';
let regex = /apples$/i
console.log(regex.test(textA)); // false
console.log(regex.test(textB)); // true
let text = 'I like APPles very much';
let regexA = /apples/;
let regexB = /apples/i;
console.log(text.search(regexA)); // -1
console.log(text.search(regexB)); // 7
let text = 'Do you like apples?';
let regex= /apples/;
// Output: apples
console.log(regex.exec(text)[0]);
// Output: Do you like apples?
console.log(regex.exec(text).input);
let text = 'Here are apples and apPleS';
let regex = /apples/gi;
// Output: [ "apples", "apPleS" ]
console.log(text.match(regex));
let text = 'This 593 string will be brok294en at places where d1gits are.';
let regex = /\d+/g
// Output: [ "This ", " string will be brok", "en at places where d", "gits are." ]
console.log(text.split(regex))
let regex = /t(e)(st(\d?))/g;
let text = 'test1test2';
let array = [...text.matchAll(regex)];
// Output: ["test1", "e", "st1", "1"]
console.log(array[0]);
// Output: ["test2", "e", "st2", "2"]
console.log(array[1]);
let text = 'Do you like aPPles?';
let regex = /apples/i
// Output: Do you like mangoes?
let result = text.replace(regex, 'mangoes');
console.log(result);
/d/s.dotAll; // => true
/d/g.global; // => true
/d/ig.flags; // => "gi"
/d/d.hasIndices; // => true
/d/i.ignoreCase; // => true
let s = "Please yes\nmake my day!";
s.match(/yes[^]*day/);
// è¿å 'yes\nmake my day'
let regex = /apples/gi;
let text = 'Here are apples and apPleS';
text.replaceAll(regex, "mangoes");
// è¿å: Here are mangoes and mangoes
| :- | - |
|---|---|
preg_match() | æ§è¡æ£å表达å¼å¹é |
preg_match_all() | æ§è¡å ¨å±æ£å表达å¼å¹é |
preg_replace_callback() | 使ç¨åè°æ§è¡æ£åè¡¨è¾¾å¼æç´¢åæ¿æ¢ |
preg_replace() | æ§è¡æ£åè¡¨è¾¾å¼æç´¢åæ¿æ¢ |
preg_split() | ææ£åè¡¨è¾¾å¼æ¨¡å¼æåå符串 |
preg_grep() | è¿å䏿¨¡å¼å¹é çæ°ç»æ¡ç® |
$str = "Visit Microsoft!";
$regex = "/microsoft/i";
// Output: Visit QuickRef!
echo preg_replace($regex, "QuickRef", $str);
$str = "Visit QuickRef";
$regex = "#quickref#i";
// Output: 1
echo preg_match($regex, $str);
$regex = "/[a-zA-Z]+ (\d+)/";
$input_str = "June 24, August 13, and December 30";
if (preg_match_all($regex, $input_str, $matches_out)) {
// Output: 2
echo count($matches_out);
// Output: 3
echo count($matches_out[0]);
// Output: Array("June 24", "August 13", "December 30")
print_r($matches_out[0]);
// Output: Array("24", "13", "30")
print_r($matches_out[1]);
}
$arr = ["Jane", "jane", "Joan", "JANE"];
$regex = "/Jane/";
// Output: Jane
echo preg_grep($regex, $arr);
$str = "Jane\tKate\nLucy Marion";
$regex = "@\s@";
// Output: Array("Jane", "Kate", "Lucy", "Marion")
print_r(preg_split($regex, $str));
Pattern p = Pattern.compile(".s", Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher("aS");
boolean s1 = m.matches();
System.out.println(s1); // Outputs: true
boolean s2 = Pattern.compile("[0-9]+").matcher("123").matches();
System.out.println(s2); // Outputs: true
boolean s3 = Pattern.matches(".s", "XXXX");
System.out.println(s3); // Outputs: false
| :- | - |
|---|---|
CANON_EQ | è§èçä»· |
CASE_INSENSITIVE | ä¸åºå大å°åçå¹é |
COMMENTS | å è®¸ç©ºæ ¼åæ³¨é |
DOTALL | åç¹æ¨¡å¼ |
MULTILINE | å¤è¡æ¨¡å¼ |
UNICODE_CASE | Unicode æç¥å¤§å°åæå |
UNIX_LINES | Unix è¡æ¨¡å¼ |
è¿ææ´å¤æ¹æ³...
æ¿æ¢å¥åï¼
String regex = "[A-Z\n]{5}$";
String str = "I like APP\nLE";
Pattern p = Pattern.compile(regex, Pattern.MULTILINE);
Matcher m = p.matcher(str);
// Outputs: I like Apple!
System.out.println(m.replaceAll("pple!"));
ææå¹é çæ°ç»ï¼
String str = "She sells seashells by the Seashore";
String regex = "\\w*se\\w*";
Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(str);
List<String> matches = new ArrayList<>();
while (m.find()) {
matches.add(m.group());
}
// Outputs: [sells, seashells, Seashore]
System.out.println(matches);
| 彿°åç§° | 说æ |
|---|---|
REGEXP | å符串æ¯å¦å¹é æ£åè¡¨è¾¾å¼ |
REGEXP_INSTR() | å¹é
æ£å表达å¼çåå符串çèµ·å§ç´¢å¼ ï¼æ³¨æï¼ä» é MySQL 8.0+ï¼ |
REGEXP_LIKE() | å符串æ¯å¦å¹é
æ£åè¡¨è¾¾å¼ (注æï¼ä» MySQL 8.0+) |
REGEXP_REPLACE() | æ¿æ¢å¹é
æ£å表达å¼çåå符串 ï¼æ³¨æï¼ä» é MySQL 8.0+ï¼ |
REGEXP_SUBSTR() | è¿åå¹é
æ£å表达å¼çåå符串 (注æï¼ä» MySQL 8.0+) |
expr REGEXP pat
mysql> SELECT 'abc' REGEXP '^[a-d]';
1
mysql> SELECT name FROM cities WHERE name REGEXP '^A';
mysql> SELECT name FROM cities WHERE name NOT REGEXP '^A';
mysql> SELECT name FROM cities WHERE name REGEXP 'A|B|R';
mysql> SELECT 'a' REGEXP 'A', 'a' REGEXP BINARY 'A';
1 0
REGEXP_REPLACE(expr, pat, repl[, pos[, occurrence[, match_type]]])
mysql> SELECT REGEXP_REPLACE('a b c', 'b', 'X');
a X c
mysql> SELECT REGEXP_REPLACE('abc ghi', '[a-z]+', 'X', 1, 2);
abc X
REGEXP_SUBSTR(expr, pat[, pos[, occurrence[, match_type]]])
mysql> SELECT REGEXP_SUBSTR('abc def ghi', '[a-z]+');
abc
mysql> SELECT REGEXP_SUBSTR('abc def ghi', '[a-z]+', 1, 3);
ghi
REGEXP_LIKE(expr, pat[, match_type])
mysql> SELECT regexp_like('aba', 'b+')
1
mysql> SELECT regexp_like('aba', 'b{2}')
0
mysql> # i: case-insensitive
mysql> SELECT regexp_like('Abba', 'ABBA', 'i');
1
mysql> # m: multi-line
mysql> SELECT regexp_like('a\nb\nc', '^b$', 'm');
1
REGEXP_INSTR(expr, pat[, pos[, occurrence[, return_option[, match_type]]]])
mysql> SELECT regexp_instr('aa aaa aaaa', 'a{3}');
2
mysql> SELECT regexp_instr('abba', 'b{2}', 2);
2
mysql> SELECT regexp_instr('abbabba', 'b{2}', 1, 2);
5
mysql> SELECT regexp_instr('abbabba', 'b{2}', 1, 3, 1);
7