markdown #bash #regex #cheatsheet #grep

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown #bash #regex #cheatsheet #grep相关的知识,希望对你有一定的参考价值。

## Quantifiers

- `*` - 0 or more
- `*?` - 0 or more non greedy
- `+` - 1 or more
- `+?` - 0 or 1 non greedy
- `?` - 0 or 1
- `??` - 0 or 1 non greedy
- `{m}` - exactly 'm'
- `{m,n}` - from m to n (`m` defaults to 0, `n` to infinity)
- `{m,n}?` - from m to n, as few as possible

## Character classes
\b - backspace(0x08)(inside[]only) 
\b - word boundary(outside[]only)  
\B - non-word boundary 
\d - digit, same as[0-9]  
\D - non-digit  
\S - non-whitespace character 
\s - whitespace character[ \t\n\r\f]  
\W - non-word character  
\w - word character[0-9A-Za-z_]

## Groups and lookaround
Positive lookahead matches a group after the main expression without including it in the result.  
`\d(?=px)`  1pt **2**px 3em **4**px
Negation lookahead specifies a group that can not match after the main expression (if it matches, the result is discarded).  
`\d(?!px)` **1**pt 2px **3**em 4px  
Positive lookbehind  
`(?<=\d)px`  matches 1pt 2**px** 3em 4**px**  
Negative lookbehind  
`(?<!\d)px`  matches 1pt 2px 3em 4px p**pt**  




`^.*\/`  grab everything before the slash. (\ is  for escaping forwardslash )  
`"[^"]*"` This will match a quoted text part without additional quotes in it  
Modern RegEx Flavors (PCRE)

## Special characters that must be escaped
Includes C, C++, Delphi, EditPad, Java, JavaScript, Perl, PHP (preg), PostgreSQL, PowerGREP, PowerShell, Python, REALbasic, Real Studio, Ruby, TCL, VB.Net, VBScript, wxWidgets, XML Schema, Xojo, XRegExp.
PCRE compatibility may vary

    Anywhere: . ^ $ * + - ? ( ) [ ] { } \ |

### Legacy RegEx Flavors (BRE/ERE)

Includes awk, ed, egrep, emacs, GNUlib, grep, PHP (ereg), MySQL, Oracle, R, sed.
PCRE support may be enabled in later versions or by using extensions

### ERE/awk/egrep/emacs

    Outside a character class: . ^ $ * + ? ( ) [ { } \ |
    Inside a character class: ^ - [ ]

### BRE/ed/grep/sed

    Outside a character class: . ^ $ * [ \
    Inside a character class: ^ - [ ]
    For literals, don't escape: + ? ( ) { } | /
    For standard regex behavior, escape: \+ \? \( \) \{ \} \|

### More examples
`(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?` **url**  
`<([a-z]+)([^<]+)*(?:>(.*)<\/\1>|\s+\/>)` **html tag**  
`([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})` **email**  
`-?\d+` **Integer**  
`(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)` **IP**




### GREP Options

	--max-count=number of matches before stopping search
	--exclude=*.txt  with -r option
*Note: Lookahead and lookbehind are Perl-style regular expression elements, so you'd have to use Perl directly, or GNU grep with the -P option, which then interprets Perl regex.*

grep -o pattern file.txt **shows only the matched string** 
grep -bn pattern file.txt **shows row and col**  
grep -v pattern file.txt **inversion**  
grep -A 3 pattern file.txt  **3+ lines after**	
grep -B 3 pattern file.txt **3+ lines before**  
grep -c pattern file.txt **count matches**  
grep -l/L pattern *.txt **list of files that matches/doesn't**  
grep -f patterns.txt file.txt **seach using patterns file**  
grep "stuff\|more" demo.txt **OR**  
grep -E "stuff|more" demo.txt **OR**  
grep -w "of" table.txt **search only for the entire word**  
grep -x "of" table.txt **search only for the entire line**  
grep -r menu /boot **recursive**  
grep '\\<kot\\>' kot.txt **word starts/end with kot**  
grep -e '--pattern' test.txt **match exact pattern**  
grep -r [-l, --binary-files=without-match]  "test" ./  **without binary files**  

### More examples

`grepc -E '(co)lor\1{2}' logs.txt` **Using backrefence to match colorcoco**  
`cat test.txt | grep --color=always -E  'GET\s/example/\?p=1'` **or** `grep 'GET\s/example/?p=1'` gives:   **GET /example/?p=1**




` echo "\ab  e\f \. " | grepc -Eo '\\.' ` finds `\a \f \.`
 



以上是关于markdown #bash #regex #cheatsheet #grep的主要内容,如果未能解决你的问题,请参考以下文章

markdown [Regex Cheatsheet] #regex

markdown Regexes ......现在你有两个问题

使用 Bash (sed?) 删除包含特定文本 (regex) 的多行 /* ... */ 样式注释

markdown [常用正则表达式]常用正则表达式总结#regex

使用bash在changelog文件中读取文件直到regex

BASH:如何在 sed 命令中对字符串使用 Regex Negative Lookahead?