From 72e1da33429ec15c277400d41e9f309a5cb137e2 Mon Sep 17 00:00:00 2001 From: Randy8080 Date: Thu, 21 Jan 2021 10:40:38 +0800 Subject: [PATCH] htmlclean --- regex.md | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/regex.md b/regex.md index 657cb77..7eca9a2 100644 --- a/regex.md +++ b/regex.md @@ -10,8 +10,7 @@ tags: categories: - Toolkit intro: | - A quick reference guide for regular expressions (regex), including symbols, ranges, grouping, assertions and some sample patterns to get you started. - This cheat sheet was made for [PCRE](https://en.wikipedia.org/wiki/Perl_Compatible_Regular_Expressions). + A quick reference for regular expressions (regex), including symbols, ranges, grouping, assertions and some sample patterns to get you started. --- Get Started {.cols-3} @@ -20,6 +19,7 @@ Get Started {.cols-3} ### Introduction This is a quick cheat sheet to getting started with regular expressions. + - [Regex in Python](#regex-in-python) _(quickref.me)_ - [Regex in JavaScript](#regex-in-javascript) _(quickref.me)_ - [Regex in PHP](#regex-in-php) _(quickref.me)_ @@ -320,9 +320,9 @@ Regex examples{.cols-3} | `ring `| Match ring springboard etc. | | `. `| Match a, 9, + etc. | | `h.o `| Match hoo, h2o, h/o etc. | -| `ring\? `| Match ing? | -| `\(quiet\) `| Match quiet) | -| `c:\\windows `| Match c:\windows | +| `ring\? `| Match ring? | +| `\(quiet\) `| Match (quiet) | +| `c:\\windows `| Match c:\windows | Use `\` to search for these special characters:
`[ \ ^ $ . | ? * + ( ) { }` @@ -363,21 +363,21 @@ In `[ ]` always escape `. \ ]` and sometimes `^ - .` ### Occurrences | Pattern | Matches | |---------------------|-----------------------------------| -| `colou?r` | match color or colour | -| `[BW]ill[ieamy's]*` | match Bill, Willy, William's etc. | -| `[a-zA-Z]+` | match 1 or more letters | -| `\d{3}-\d{2}-\d{4}` | match a SSN | -| `[a-z]\w{1,7}` | match a UW NetID | +| `colou?r` | Match color or colour | +| `[BW]ill[ieamy's]*` | Match Bill, Willy, William's etc. | +| `[a-zA-Z]+` | Match 1 or more letters | +| `\d{3}-\d{2}-\d{4}` | Match a SSN | +| `[a-z]\w{1,7}` | Match a UW NetID | ### Greedy versus lazy | Pattern | Meaning | |---------------------------|----------------------------------| -| `* + {n,}`
_greedy_ | match as much as possible | -| `<.+> ` | finds 1 big match in \bold\<\/b> | -| `*? +? {n,}?`
_lazy_ | match as little as possible | -| `<.+?>` | finds 2 matches in \<b>bold\<\/b> | +| `* + {n,}`
_greedy_ | Match as much as possible | +| `<.+> ` | Finds 1 big match in \bold\<\/b> | +| `*? +? {n,}?`
_lazy_ | Match as little as possible | +| `<.+?>` | Finds 2 matches in \<b>bold\<\/b> | ### Scope {.col-span-2} @@ -424,9 +424,9 @@ Can lead to catastrophic backtracking. ### Back references | Pattern | Matches | |--------------------------|--------------------------------------------------| -| `(to) (be) or not \1 \2` | match to be or not to be | -| `([^\s])\1{2}` | match non-space, then same twice more   aaa, ... | -| `\b(\w+)\s+\1\b` | match doubled words | +| `(to) (be) or not \1 \2` | Match to be or not to be | +| `([^\s])\1{2}` | Match non-space, then same twice more   aaa, ... | +| `\b(\w+)\s+\1\b` | Match doubled words | @@ -442,8 +442,8 @@ Use non-capturing or atomic groups when possible ### Atomic groups | Pattern | Meaning | |------------------------|----------------------------| -| `(?>red\|green\|blue)` | faster than non-capturing | -| `(?>id\|identity)\b` | match id, but not identity | +| `(?>red\|green\|blue)` | Faster than non-capturing | +| `(?>id\|identity)\b` | Match id, but not identity | "id" matches, but `\b` fails after atomic group, parser doesn't backtrack into group to retry 'identity'