我有一个文本文件如下:
#1xx Informational responses
100=Continue
101=Switching Protocols
102=Processing
#2xx Success
200=OK
201=Created
202=Accepted
203=Non-Authoritative Information
204=No Content
205=Reset Content
206=Partial Content
207=Multi-Status
208=Already Reported
226=IM Used
Run Code Online (Sandbox Code Playgroud)
我想要做的是将其转换为下面的一个:
#1xx_Informational responses
100=Continue
101=Switching_Protocols
102=Processing
#2xx_Success
200=OK
201=Created
202=Accepted
203=Non_Authoritative_Information
204=No_Content
205=Reset_Content
206=Partial_Content
207=Multi_Status
208=Already_Reported
226=IM_Used
Run Code Online (Sandbox Code Playgroud)
我可以用:
:%s/[ \|-]/_/g
Run Code Online (Sandbox Code Playgroud)
但它无条件地在所有行中改变空格或_和_,包括第一行:
#1xx_Informational_responses
Run Code Online (Sandbox Code Playgroud)
我不要那个.相反,我想要实现的是只有当一行以数字开头时才用_替换空格和-s.对于所有其他线路,我不想执行替换操作.
有没有办法"有条件地"执行更换操作?我不想处理行号,行号可能会改变.我想要关于线的内容的答案(从这种情况的数字开始),而不是行号.
提前致谢.
:g/^\d/s/[ -]/_/g
Run Code Online (Sandbox Code Playgroud)
该:global
命令在给定正则表达式匹配的每一行上执行命令.
(顺便说一句,你的正则表达式[ \|-]
匹配一个空格\
或|
或者-
.)