当前位置:网站首页>VIM search and replacement and the use of regular expressions
VIM search and replacement and the use of regular expressions
2022-07-25 05:19:00 【weixing100200】

abc Replace with abcdd
abc123342343242xyz
14242abc23424dfsfxyz
~
:%s#\(abc\)#\1dd#g
%s# \(abc\) #\1dd #g
hold abc Replace as a group with 1dd, namely abcdd
hold abc Replace with adminabcdd
adminabcdd123342343242xyz
14242adminabcdd23424dfsfxyz
~
~
:%s#\(abc\)#admin\1dd#g
Put all strings in the text abc.......xyz Replace with xyz......abc
The first way to write it :
adminabcdd123342343242xyz
14242adminabcdd23424dfsfxyz
~
~
:%s/abc\(.*\)xyz/xyz\1abc/g
The second way :
adminabcdd123342343242xyz
14242adminabcdd23424dfsfxyz
:%s/\(abc\)\(.*\)\(xyz\)/\3\2\1/g
## Between the first and third lines abc Replace with abcdd
abc123342343242xyz
abc242423
14223abc###1432
14242abc23424dfsfxyz
:1,3s#\(abc\)#\1dd#g
Delete the beginning of the line ## ( Group replacement )
#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
## Delete the beginning of the line #(# No. substitution is empty )
#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
With # Replace the beginning with empty , That is to delete the beginning #
# 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
Replace the beginning of the line with #
#abc123342343242xyz
#abc242423
#14223abc###1432
#
#
#
#
#14242abc23424dfsfxyz
#
#
~
:%s/^/#/g
##UUID The first line is added # Number
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 Add at the beginning # Number ( Group writing )
UUID=f5954a19-b069-42e4-a6ca-3a1cd06d0735 / ext4 defaults 1 1
~
:%s/^\(UUID\)/#\1/g
## Group and or
[[email protected] ~]# egrep -v '^#|^$' /etc/nginx/nginx.conf | wc -l
84
[[email protected] ~]# grep -v '^\(#\|$\)' /etc/nginx/nginx.conf |wc -l
84
Filter IP Address :

Simple substitution expressions
:[range]s/from/to/[flags]
range: Search scope , If no range is specified , It acts on but moves forward .:1,10s/from/to/Said in the first 1 To the first 10 That's ok ( Including 1, The first 10 That's ok ) Search and replace ;:10s/from/to/Indicates only in the second row 10 Line search Replacement ;:%s/from/to/Means to search for replacement in all rows ;1,$s/from/to/ditto .
flagsThere are four options :cconfirm, Ask... Before each replacement ;eerror, Don't show errors ;ggloble, Don't ask , Full line replacement . If notgOptions , Only the first matching string of each line is replaced ;iignore, Ignore case .
These options can be combined , Such as
cgiIndicates case insensitive , Full line replacement , Ask before replacing .
Regular expressions
Metacharacters
Metacharacters
Metacharacters explain . Match any character [abc] Match any one of the characters in the formula brackets , You can use -Indicates the character range . Such as [a-z0-9] Match lowercase letters and numbers[^abc] Matches any character except those in square brackets \d Match Arabic numerals , Equate to [0-9] \D Match any character other than Arabic numerals , Equate to [^0-9] \x Match hexadecimal numbers , Equate to [0-9A-Fa-f] \X Matches any character other than a hexadecimal digit , Equate to [^0-9A-Fa-f] \l matching [a-z] \L matching [^a-z] \u matching [A-Z] \U matching [^A-Z] \w Match word letters , Equate to [0-9A-Za-z_] \W Match any character other than the letter of the word , Equate to [^0-9A-Za-z_] \t matching <TAB>character\s Matching blank character , Equate to [\t] \S Match non white space characters , Equate to [^\t] Some ordinary characters need to be changed
Metacharacters explain \* matching *character. matching .character\/ matching /character\ matching \character\[ matching [character\] matching ]characterA metacharacter representing a quantity
Metacharacters explain * matching 0- Any one \+ matching 1- Any one \? matching 0-1 individual \{n,m} matching n-m individual \{n} matching n individual \{n,} matching n- Any one \{,m} matching 0-m individual A metacharacter representing a position
Metacharacters explain $ Match the end of the line ^ Match the beginning of the line \< Match the beginning of the word \> Match word endings
Replace variable
In the regular form, use
\(and\)Enclosed regular expressions , It can be used later\1、\2And other variables\(and\)The content in .
Example
- Delete the space at the end of the line :
:%s/\s+$//g - Delete the extra space at the beginning of the line :
%s/^\s*//perhaps%s/^ *// - Delete empty lines without content :
%s/^$//perhapsg/^$/d - Delete blank lines with spaces :
%s/^\s*$//perhapsg/^\s*$/d - Delete with spaces or TAB Blank lines from beginning to end :
%s/^[ |\t]*$//perhapsg/^[ |\t]*$/d Put all the strings in the text “abc……xyz” Replace with “xyz……abc” It can be written as follows
:%s/abc\(.*\)xyz/xyz\1abc/g :%s/\(abc\)\(.*\)\(xyz\)/\3\2\1/g
边栏推荐
- Leetcode55. Jumping game
- Implementation principle of epoll
- nacos中哪边有这个列的sql脚本啊?
- Anshi semiconductor management paid a visit to Wentai technology and Gree Electric appliance
- The price is 17300! Why does Huawei mate x face Samsung fold?
- 一篇文章带你读懂Redis的哨兵模式
- RHCE first day
- 如何判断是否遭到DDOS攻击
- Performance Optimization: lazy loading of pictures
- Blog Description & message board
猜你喜欢
随机推荐
JWT(json web token)
Compile ue5.0
TCL shows a number of folding screen mobile phones: the screen and hinge are independently developed!
Redis cluster setup (Windows)
deep报错
ZTE's first 5g mobile phone, axon 10 pro, was launched in the first half of this year
How do novices open accounts for stock speculation? Is it safe for securities companies to open accounts online?
基于云原生的私有化 PaaS 平台交付实践
2022-7-18 summary
STL notes (VII): container deque
Necessary skills for mobile terminal test: ADB command and packet capturing
1310_一个printf的实现分析
STM32 development note 120: solve the problem that%f in printf cannot be output
Redis的三个必知必会的问题
ping命令
Why does the official not recommend using UUID as MySQL primary key
2022-7-15 summary
Performance Optimization: lazy loading of pictures
学习记录[email protected]研发效能度量指标
STM32 development note 117: generate IIR low-pass filter coefficients using MATLAB

![[untitled]](/img/6c/df2ebb3e39d1e47b8dd74cfdddbb06.gif)








