\<和 \>用于匹配word,但grep中的word不考虑标点,只考虑26个字母:
grep --color "\<ab\>" data.txt
hello (ab)
hello ab
(ab)和ab都match了
| character | grep | egrep | find | wildcard |
|---|---|---|---|---|
| * | >=0个前一个字符 | >=0个前一个字符 | >=0个前一个字符 | >=0个任意字符. ls m*l will get: ml mal mbl m.l |
| 指定m~n个 | 匹配g[2~3个o]d grep "go\{2,3\}d" regex.txt grep --color "\<c\{3\}99" data.txt # just c 3 times: aa ccc99 bb grep --color "\<c\{3,\}99" data.txt # c [3, infinity]: aa ccc99 bb aa cccc99 bb |
egrep "go{2,3}d" regex.txt | find -regextype egrep -regex ".*go{2,3}d.*" | |
| + 仅egrep | ≥1个前一个RE字符: egrep --color 'go+d' data.txt good god gooood |
|||
| ? 仅egrep | 0或1个前一个RE字符: egrep --color 'go?d' data.txt god gd |
|||
| ()+ 仅egrep | ≥1个前一个组 egrep --color 'A(xyz)+C' data.txt AxyzxyzC AxyzxyzxyzC |
|||
| () group 仅egrep | egrep --color 'g(la|oo)d' data.txt glad good |
|||
| [] | []中的任意 egrep --color "4[89]3" data.txt 483 Sept 5AP1996 USP 65.00 LVX2C 189 如果需要匹配[]本身,需要\[和\]: grep --color "\[file\]" data.txt [file] |
范围(数字,字母) grep --color "5..199[6,8]" data.txt 483 Sept 5AP1996 USP 65.00 LVX2C 189 483 may 5PA1998 USP 37.00 KVM9D 644 |
1:单个列出. 2:指定范围 ls a[b,c,d].doc ab.doc ac.doc ad.doc ls a[b-z].doc ab.doc ae.doc az.doc |
|
| ? | 一个任意字符 ls a?c abc a.c axc |
|||
| {} | terms are separated by commas and each term must be the name of something or a wildcard. ls {*.doc,*.pdf} a.doc b.doc c.pdf e.pdf 1:{}中不要有一个空格 2:{}项目个数至少两个,只有一个term又何必用{}? |
| item | php perl compitable |
|---|---|
| * | >=0个前个re字符 |
| + | >=1个前个re字符 |
| ? | 0或1个前个re字符 |
| 对反斜线'\'的转义 |
$content="a\\\\b"; echo "content length=".strlen($content)."\n"; echo $content."\n"; $pattern="/a\\\\{2}b/"; /*$pattern="/a\\\\\\\\b/";*/如果不用范围,匹配2个\需要8个 echo "pattern length=".strlen($pattern)."\n"; echo $pattern."\n"; if(preg_match($pattern, $content,$matches)){echo __LINE__; print_r($matches);} 输出: content length=4 a\\b pattern length=9 /a\\{2}b/ 9Array ( [0] => a\\b ) |
| character | MySQL |
|---|---|
| % | >=0个任意字符 |
| _:下划线 | 1个任意字符 |
https://www.gnu.org/software/sed/manual/html_node/Regular-Expressions.html
sed 去掉开头的空格
sed -i 's/^[ ]\{1,\}//g' xy.txt
sed需要转义的:^$.*\[
Letters, digits and (){}+?| must not be quoted (you can get away with quoting some of these in some implementations).
The sequences \(, \), \n, and in some implementations
\{, \}, \+, \?, \| and other backslash+alphanumerics have special meanings.:
\( \)分组, \{ \}用于前一RE字符出现次数:
D.Gen[ice@ name]echo "|123787877878788787|ABCDEF" | sed 's/|\([0-9]\{1,\}\)|\([A-Z]\{1,\}\)/_\2_\1/g'
_ABCDEF_123787877878788787
\+
D.Gen[ice@ name]echo "|123787877878788787|ABCDEF|" | sed 's/|\([0-9]\+\)|\([A-Z]\+\)|/_\2_\1/g'
_ABCDEF_123787877878788787
关于list:
/* 其实记这些非特殊字符没有意义,因为非特殊太多了,应该记特殊字符
The characters $, ., *,\ and [ are normally not special within list.
$ :
D.Gen[ice@ name]echo "|\$|" | sed 's/|[$]|/MM/g'
MM
.:
D.Gen[ice@ name]echo "_._" | sed 's/_[.]_/MM/g'
MM
*:
D.Gen[ice@ name]echo "_*_" | sed 's/_[*]_/MM/g'
MM
\
echo "_\\_" | sed 's/_[\]_/MM/g'
MM
[
D.Gen[root@ stock]#echo "_[_" | sed 's/_[[]_/MM/g'
MM
*/
特殊情况
1:
To include ] in the list, make it the first character (after the ^ if needed),
D.Gen[root@ stock]#echo "]]ab]]ab" | sed 's/[]]/_/g'
__ab__ab
2:
to include ^ put it after the first character.
D.Gen[root@ stock]#echo "^^ab^^ab" | sed 's/[|^]/./g'
..ab..ab
3:
, to include - in the list, make it the first or last, but ] has occupy first, so - should occupy last
D.Gen[root@ stock]#echo "--ab--ab" | sed 's/[a-]/_/g'
___b___b
]:
D.Gen[root@ stock]#echo "]_]_" | sed 's/[]]/MM/g'
MM_MM_
D.Gen[root@ stock]#echo "]_]_" | sed 's/[^]]/MM/g'
| character | Python RE |
|---|---|
| (pattern)? | 0,1个pattern |
| (pattern)* | >=0个前一个pattern |
| (pattern)+ | >=1个前一个pattern |
| (pattern){m,n} | >=m个, <=n个pattern |