当前位置:网站首页>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
边栏推荐
- STM32 Development Notes 121: Kalman filter I understand
- ping命令
- 自己实现is_class
- Your technical leader doesn't understand this? Without it, there is no complete thinking process of design
- FinClip实现微信授权登录的三种方案
- [no title] 1
- Blog Description & message board
- What should testers do if they encounter a bug that is difficult to reproduce?
- Three must know and know problems of redis
- panda3d 键盘移动场景
猜你喜欢

An article takes you to understand the sentinel mode of redis

初步了解Panda3d粒子系统

panda3d 键盘移动场景

Implement is by yourself_ convertible

Introduction to kubernetes

1310_一个printf的实现分析

Androd releases jitpack open source project (gradle7.2)

odoo14 | 关于状态栏statusbar关键词使用后显示异常及解决方法

Project management tools - project developer tools

Implement is by yourself_ base_ of
随机推荐
Ping command
基于云原生的私有化 PaaS 平台交付实践
Project management tools - project developer tools
Project management tool - Introduction and practice of Alibaba cloud projex
Blog Description & message board
项目管理工具——项目开发者工具
Thesis reading | which is the best multilingual pre training technology for machine translation? See the latest progress!
panda3d 键盘移动场景
Implement is by yourself_ convertible
Redis cluster setup (Windows)
教你三招从让性能从20s优化到500ms
Luogu p2391 snow covered problem solution
China trifluoroethanol industry research and investment forecast report (2022 Edition)
FinClip实现微信授权登录的三种方案
基环树入门
85 distributed project construction
Guanghetong and Intel released the global version of 5g communication module
Introduction to base ring tree
Learning records [email protected] R & D effectiveness measurement indicators
nacos中哪边有这个列的sql脚本啊?
