有下面一个字符串,我想用vim替换单词中的空格,但是单词与单词之间的空格需要保留下来。
源字符串:
Timb erland Men's 6" Pr emium Bo ot
Timberl and Women's Wate rproff Boot
Timberland Men's Fiel d Boot
Tim berland PRO Premium Boo t
正确的字符串:
Timberland Men's 6" PremiumBoot
Timberland Women's WaterproffBoot
Timberland Men's Field Boot
Timberland PRO Premium Boot
规律如下:
如果空格两边的字母都是小写字母,那么这个空格应该去掉:[a-z]\s[a-z] ---> [a-z][a-z]
如果空格两边字母有一个是大写的或者数字,那么这个空格是单词与单词之间的分隔符,应该保留。
已经尝试了的方案:
1,全文替换空格,这样会把单词与单词之间的分隔符也替换掉。
2,用vim里面的%s/\<[a-z]\s[a-z]\>//g,失败
正则表达式
vim
空格
替换
------解决方案--------------------
%s/[a-z]\@<=\s\+[a-z]\@=//g
------解决方案--------------------
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script type="text/javascript">
var arr=["Timb erland Men's 6\" Pr emium Bo ot","Timberl and Women's Wate rproff Boot","Timberland Men's Fiel d Boot","Tim berland PRO Premium Boo t"];
var reg=/[a-z]\s+[a-z]/g;
for(var i in arr){
arr[i]=arr[i].replace(reg,change);
}
for(var i in arr){
alert(arr[i]);
}
function change(a){
return a.replace(/\s+/,"");
}
</script>
</head>
<body>
</body>
</html>
这样试试
------解决方案--------------------
var s = "Timb erland Men's 6\" Pr emium Bo ot\n"+
"Timberl and Women's Wate rproff Boot\n"+
"Timberland Men's Fiel d Boot\n"+
"Tim berland PRO Premium Boo t"
var reg = /[a-z]+(\s+)[a-z]/g;
function replace(str){
if(reg.test(str)){
str = str.replace(reg,function(){
return arguments[0].replace(/\s+/,"");
});
str = replace(str);
}
return str;
}
s = replace(s).replace(/\s{2,}/g," ");
console.log(s);
------解决方案--------------------
var s = (
"Timb erland Men's 6\" Pr emium Bo ot\n"
+ "Timberl and Women's Wate rproff Boot\n"
+ "Timberland Men's Fiel d Boot\n"
+ "Tim berland PRO Premium Boo t"