Excel多种方法在中文和英文交界处插入一个空格
需求:批量替换,要把下面的这个字符串中中文和英文交界处插入一个空格,中英文不会互相隔开,每一个字符串中都是前面中文,后面英文。,"交通规划Transport Planning"
用普通公式就可以解决:作者:swl4561255=REPLACE(A1,LENB(A1)-LEN(A1)+1,," ")
VBA正则表达式代码1:作者:swl4561255Sub aa() With CreateObject("VBscript.regexp") .Pattern = "[\u4e00-\u9fa5]+" Range("A1") = .Execute(Range("A1"))(0) & .Replace(Range("A1"), " ") End WithEnd Sub多个单元格可以外套循环解决。VBA正则表达式代码2:作者:香川群子
不就是找到英文字母就加个空格么,正则用自定义函数方法更简单。Function TQ(txt)
Set Reg = CreateObject("VBScript.RegExp") With Reg .Pattern = "([a-zA-Z])" TQ = .Replace(txt, " $1") End WithEnd Function
VBA 代码3:作者:香川群子其实不用正则也很简单,用like方法即可:Function TR(txt) For i = 1 To Len(txt) If Mid(txt, i, 1) Like "[A-Za-z]" Then TR = Left(txt, i - 1) & " " & Mid(txt, i): Exit For NextEnd FunctionVBA 正则代码4:作者:龙城飞将IIIFunction InsertSpace(Rng As Range) Dim Reg Dim MyStr As String Set Reg = CreateObject("vbscript.regexp") With Reg .Pattern = "(\W+)(\w+\s+)" .Global = True MyStr = Reg.Replace(Rng, "$1 $2") End With InsertSpace = MyStrEnd FunctionVBA 正则代码5:作者:zhouzhongchi练习:With re .Pattern = "(\w)" Debug.Print .Replace(s, " $1")
.Pattern = "(.(?=\w))" Debug.Print .Replace(s, "$1 ")End With |