当前位置:网站首页>Vim查找替换及正则表达式的使用
Vim查找替换及正则表达式的使用
2022-07-25 05:19:00 【weixing100200】

abc替换为abcdd
abc123342343242xyz
14242abc23424dfsfxyz
~
:%s#\(abc\)#\1dd#g
%s# \(abc\) #\1dd #g
把abc作为一个分组替换为1dd,即abcdd
把abc替换为adminabcdd
adminabcdd123342343242xyz
14242adminabcdd23424dfsfxyz
~
~
:%s#\(abc\)#admin\1dd#g
把文中所有字符串abc.......xyz替换为xyz......abc
第一种写法:
adminabcdd123342343242xyz
14242adminabcdd23424dfsfxyz
~
~
:%s/abc\(.*\)xyz/xyz\1abc/g
第二种写法:
adminabcdd123342343242xyz
14242adminabcdd23424dfsfxyz
:%s/\(abc\)\(.*\)\(xyz\)/\3\2\1/g
##第一行到第三行之间abc替换为abcdd
abc123342343242xyz
abc242423
14223abc###1432
14242abc23424dfsfxyz
:1,3s#\(abc\)#\1dd#g
删除行首## (分组替换)
#location / {
# proxy_pass http://139.198.40.94;
# proxy_redirect off;
# proxy_set_header Host $http_host;
# proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# proxy_set_header X-Forwarded-Proto https;
# index index.html index.htm index.php;
# autoindex on;
# }
:%s/^#\(.*\)/\1/g
##删除行首#(#号替代为空)
#location / {
# proxy_pass http://139.198.40.94;
# proxy_redirect off;
# proxy_set_header Host $http_host;
# proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# proxy_set_header X-Forwarded-Proto https;
# index index.html index.htm index.php;
# autoindex on;
# }
:%s/^#/ /g
以#开头的替换为空,也就是删除开头的#
# autoindex on;
# }
#
#location @handle_redirects {
# set $saved_redirect_location '$upstream_http_location';
# proxy_pass $saved_redirect_location;
#}
#
# # Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
:%s/\#\(.*\)/\1/g
行首全部替换为#
#abc123342343242xyz
#abc242423
#14223abc###1432
#
#
#
#
#14242abc23424dfsfxyz
#
#
~
:%s/^/#/g
##UUID开头的行增加#号
UUID=f5954a19-b069-42e4-a6ca-3a1cd06d0735 / UUID
#UUID=f5954a19-b069-42e4-a6ca-3a1cd06d0735 / ext4 defaults 1 1
#UUID=f5954a19-b069-42e4-a6ca-3a1cd06d0735 / ext4 defaults 1 1
#UUID=f5954a19-b069-42e4-a6ca-3a1cd06d0735 / ext4 defaults 1 1
#UUID=f5954a19-b069-42e4-a6ca-3a1cd06d0735 / ext4 defaults 1 1
#UUID=f5954a19-b069-42e4-a6ca-3a1cd06d0735 / ext4 defaults 1 1
~
~
:%s/^UUID/#UUID/g
##UUID开头加#号(分组写法)
UUID=f5954a19-b069-42e4-a6ca-3a1cd06d0735 / ext4 defaults 1 1
~
:%s/^\(UUID\)/#\1/g
##分组和或
[[email protected] ~]# egrep -v '^#|^$' /etc/nginx/nginx.conf | wc -l
84
[[email protected] ~]# grep -v '^\(#\|$\)' /etc/nginx/nginx.conf |wc -l
84
过滤IP地址:

简单替换表达式
:[range]s/from/to/[flags]
range:搜索范围,如果没有指定范围,则作用于但前行。:1,10s/from/to/表示在第1到第10行(包含第1,第10行)之间搜索替换;:10s/from/to/表示只在第10行搜索替换;:%s/from/to/表示在所有行中搜索替换;1,$s/from/to/同上。
flags有如下四个选项:cconfirm,每次替换前询问;eerror, 不显示错误;ggloble,不询问,整行替换。如果不加g选项,则只替换每行的第一个匹配到的字符串;iignore,忽略大小写。
这些选项可以合并使用,如
cgi表示不区分大小写,整行替换,替换前询问。
正则表达式
元字符
元字符
元字符 说明 . 匹配任意字符 [abc] 匹配方括号中的任意一个字符,可用 -表示字符范围。如[a-z0-9]匹配小写字母和数字[^abc] 匹配除方括号中字符之外的任意字符 \d 匹配阿拉伯数字,等同于[0-9] \D 匹配阿拉伯数字之外的任意字符,等同于[^0-9] \x 匹配十六进制数字,等同于[0-9A-Fa-f] \X 匹配十六进制数字之外的任意字符,等同于[^0-9A-Fa-f] \l 匹配[a-z] \L 匹配[^a-z] \u 匹配[A-Z] \U 匹配[^A-Z] \w 匹配单词字母,等同于[0-9A-Za-z_] \W 匹配单词字母之外的任意字符,等同于[^0-9A-Za-z_] \t 匹配 <TAB>字符\s 匹配空白字符,等同于[\t] \S 匹配非空白字符,等同于[^\t] 一些普通字符需转意
元字符 说明 \* 匹配 *字符. 匹配 .字符\/ 匹配 /字符\ 匹配 \字符\[ 匹配 [字符\] 匹配 ]字符表示数量的元字符
元字符 说明 * 匹配0-任意个 \+ 匹配1-任意个 \? 匹配0-1个 \{n,m} 匹配n-m个 \{n} 匹配n个 \{n,} 匹配n-任意个 \{,m} 匹配0-m个 表示位置的元字符
元字符 说明 $ 匹配行尾 ^ 匹配行首 \< 匹配单词词首 \> 匹配单词词尾
替换变量
在正则式中以
\(和\)括起来的正则表达式,在后面使用的时候可以用\1、\2等变量来访问\(和\)中的内容。
例子
- 删除行尾空格:
:%s/\s+$//g - 删除行首多余空格:
%s/^\s*//或者%s/^ *// - 删除沒有內容的空行:
%s/^$//或者g/^$/d - 删除包含有空格组成的空行:
%s/^\s*$//或者g/^\s*$/d - 删除以空格或TAB开头到结尾的空行:
%s/^[ |\t]*$//或者g/^[ |\t]*$/d 把文中的所有字符串“abc……xyz”替换为“xyz……abc”可以有下列写法
:%s/abc\(.*\)xyz/xyz\1abc/g :%s/\(abc\)\(.*\)\(xyz\)/\3\2\1/g
边栏推荐
- What should testers do if they encounter a bug that is difficult to reproduce?
- VPP不能加载UP状态接口
- Project management tool - Introduction and practice of Alibaba cloud projex
- Performance Optimization: how to solve the slow loading speed of the first screen of spa single page application?
- Deep error
- STL notes (VIII): container - List
- 2022-07-24:以下go语言代码输出什么?A:[]int{};B:[]int(nil);C:panic;D:编译错误。 package main import ( “fmt“ ) f
- The third question of force deduction -- the longest substring without repeated characters
- How do novices open accounts for stock speculation? Is it safe for securities companies to open accounts online?
- Summary and Prospect of aut, the transport layer protocol of sound network -- dev for dev column
猜你喜欢

自己实现is_convertible

When image component in wechat applet is used as background picture

This low code reporting tool is needed for data analysis
Set up private CA server
![2022-07-24:以下go语言代码输出什么?A:[]int{};B:[]int(nil);C:panic;D:编译错误。 package main import ( “fmt“ ) f](/img/bf/e38a8fd813f88a83f61a1abfa3b95d.png)
2022-07-24:以下go语言代码输出什么?A:[]int{};B:[]int(nil);C:panic;D:编译错误。 package main import ( “fmt“ ) f

Thesis reading | which is the best multilingual pre training technology for machine translation? See the latest progress!

Wechat applet related operation examples

What should testers do if they encounter a bug that is difficult to reproduce?

HMS Core Discovery第16期直播预告|与虎墩一起,玩转AI新“声”态

Redis的三个必知必会的问题
随机推荐
HMS Core Discovery第16期直播预告|与虎墩一起,玩转AI新“声”态
Panda3D keyboard moving scene
ping命令
Compile ue5.0
Information System Project Manager --- Chapter IX examination questions of project human resource management over the years
Implement is by yourself_ convertible
Your technical leader doesn't understand this? Without it, there is no complete thinking process of design
STL notes (II): template and operator overloading
Ownership in rust -- introduction of rust language Xiaobai 11
学习记录[email protected]研发效能度量指标
Build keyword driven automated testing framework
The price is 17300! Why does Huawei mate x face Samsung fold?
Solve the problem that uni app applet obtains routes and route parameters
服务器防护的七个建议
Harbor installation
使用getifaddrs获取本机网口IP地址
STL notes (VIII): container - List
2022-7-13 summary
一篇文章带你读懂Redis的哨兵模式
What about reinstalling win11 system?
