regex

There’s a great blog post about Lookahead/Lookbehind(先行断言,后行断言)

Oracle API about java/util/regex/Pattern.html

Read config file with regex:

target file: updatedb.conf

 PRUNE_BIND_MOUNTS ="yes"
# PRUNENAMES=".git .bzr .hg .svn"
PRUNEPATHS="/tmp /var/spool /media"
PRUNEFS =  "NFS"

grep test:

$ grep -Po "(?<=PRUNEPATHS=).*" updatedb.conf

结果

"/tmp /var/spool /media"

Find a tag name in xml:(with bug)

grep -P "(?<=<)[^<>/]+(?=>)" /etc/cupshelpers/preferreddrivers.xml

前面有<, 中间没有<>?, 后面有>, 匹配中间
bug:
不能区分,和注释

grep -P "(?<=<)[^< >/]+(?=( |>))" /etc/cupshelpers/preferreddrivers.xml

前面有<, 中间没有< >?, 后面有>或者SPACE, 匹配中间
bug:
不能过滤注释

?=可以用来分组,但不捕获

grep -P "^[-+]?\d*\.?\d*$" list
list
7.
5.56
0.123
1.23
5
77654
-3
-2.0
-0.33
+833
0.0.2
.23
advg
-adf
-3.f
-f.3
+3.f
+f.3
.
+kj33f

bug: 还能匹配到.

git filter-branch –commit-filter ‘export GIT_AUTHOR_NAME=”cfig”; git commit-tree “$@”‘
git update-ref -d refs/original/refs/heads/master