史上最全的正则表达式多行匹配例子 from karlzheng@csdn
2012-10-28 08:41
211 查看
经常碰到想查找多行匹配的时候,比如:有一份log如下:
想实现git log --author zhengkl的功能:
当然最方便的还是vim命令:
commit 0b79912f43f110f64f60aba1244c71c4b0656b4c Author: xxx <xxx@758d13f7-6a34-8146-8d86-37c7f849097d> Date: Mon Oct 22 09:26:30 2012 +0000 usb输出更多的调试信息 commit e1d14cca93a417327d78a40a2787cb137ef8ef1d Author: zhengkl <zhengkl@758d13f7-6a34-8146-8d86-37c7f849097d> Date: Mon Oct 22 08:58:04 2012 +0000 修改modem hsic_net_data_handler处理函数 commit e02c4740435cd4cbbd1ee7e31b513ffcb03ea906 Author: xxx <xxx@758d13f7-6a34-8146-8d86-37c7f849097d> Date: Mon Oct 22 07:45:02 2012 +0000 lcd add gamma_setting version 1 for ID code 0x1h.
想实现git log --author zhengkl的功能:
vim脚本: func! MyFindEndLine(linenr) let l:buf_end_line = line('$') let l:line = a:linenr while l:line <= l:buf_end_line let l:line += 1 let l:line_buf = getline(l:line) if matchstr(l:line_buf, "^commit") != "" return (l:line - 1) en endw return l:buf_end_line endf func! MyFindTextBlockFunc(string) let g:mylist = [] exec "g /".a:string."/call add(g:mylist, line('.'))" let l:text = [] for i in g:mylist let l:end_line = MyFindEndLine(i) call extend(l:text, getline(i - 1, l:end_line)) call writefile(l:text, "test.txt") endfo endf :call MyFindTextBlockFunc("zhengkl")
awk脚本: #!/bin/awk -f { is_end_file = 1 if (match($0, "commit")) { a = $0 do { is_end_file = !getline if (match($0, "zhengkl")) { print a print $0 is_end_file = !getline while (! match($0, "commit")) { print $0 is_end_file = getline a = $0 } } else { is_end_file = !getline a =$0 } } while(is_end_file != 1); } }
sed脚本: /^commit/ { : get_commit h n /zhengkl/ { H x p : readagain n /^commit/ { b get_commit } p b readagain } }
当然最方便的还是vim命令:
g/zhengkl/-1;,/commit/-1 write >> test.txt
相关文章推荐
- php 正则表达式匹配中文的例子
- js正则表达式详解-子匹配 匹配原理 例子讲解
- 正则表达式找程序实用例子:正则表达式匹配不报含指定字符串,匹配后面跟着指定字符串
- 地球上最全的正则表达式匹配
- 正则表达式匹配的一个例子
- Scala - 正则表达式匹配例子
- python非贪婪、多行匹配正则表达式例子
- [Python]几个正则表达式匹配的例子
- 从一个匹配数字和字母密码的正则表达式的例子到零宽断言
- 模糊匹配字符的正则表达式+例子
- 2017-08-31截止 手机最全的正则匹配表达式
- 正则表达式(四) 部分例子,匹配规则
- python非贪婪、多行匹配正则表达式例子[转载]
- (学习)python非贪婪、多行匹配正则表达式例子
- java 通过正则表达式获取匹配字符串的例子
- 一个匹配URL的正则表达式例子(带详细解释)
- (学习)python非贪婪、多行匹配正则表达式例子 分类: 正则表达式 2013-08-19 11:21 2540人阅读 评论(2) 收藏
- 正则表达式匹配form表单中所有内容的例子
- (学习)python非贪婪、多行匹配正则表达式例子
- Python正则表达式匹配中文