求大神帮写一个正则表达式
求大神帮写一个正则表达式,我自己不太懂这个!Sub 正则替换()
r = Cells(Rows.Count, 2).End(xlUp).Row
ar = Range("B2").Resize(r, 2)
Set reg = CreateObject("VBScript.RegExp")
reg.Global = True
reg.Pattern = "[^\w一-龥 *]+"
For i = 1 To r
ar(i, 1) = LCase(reg.Replace(ar(i, 1), ""))
Next
reg.Pattern = "^([\d\D]*?)(\w[^一-龥]+)([\d\D]+)$"
For i = 1 To r - 1
ar(i, 2) = reg.Replace(ar(i, 1), "$2")
ar(i, 1) = reg.Replace(ar(i, 1), "$1$3$2")
Next
'Range("B2").Resize(r, 2) = ar
Range("L2").Resize(r, 2) = ar '为对照,把处理结果写入L列,不覆盖原数据
End Sub 正则不会,用power query硬解出来,不过去除符号还没好的想法,只能用穷举
非常感谢大佬
还有几个小问题,比如:泉芯家乡花生汤圆400g会成 泉芯家乡花生汤圆g400
艾可思奖杯鸭太空杯500ML SFC-616 会成 艾可思奖杯鸭太空杯6500ml sfc61
这个可不可以再优化一下呢,如果碰到有数字与英文的时候,就按原来的顺序提取可以吗! Sub 正则替换()
r = Cells(Rows.Count, 2).End(xlUp).Row
ar = Range("B2").Resize(r, 2)
Set reg = CreateObject("VBScript.RegExp")
reg.Global = True
reg.Pattern = "[^\w一-龥 \*]+"
For i = 1 To r - 1
ar(i, 1) = reg.Replace(LCase(ar(i, 1)), "")
Next
reg.Pattern = "^(\W*)(\w[\w\* 包袋盒支片]*)([\d\D]*)$"
For i = 1 To r - 1
ar(i, 2) = reg.Replace(ar(i, 1), "$2")
ar(i, 1) = reg.Replace(ar(i, 1), "$1$3$2")
Next
Range("L2").Resize(r, 2) = ar
End Sub 太完美了,多想要的效果, \d+\w+(\s)?(\s)?(\w+)?-?(\d+)?(\*\d+[包袋]?)?(\*\d+[包袋]?)? Sub RegExp()
Dim RegExp, i%, Match
For i = 2 To Range("B" & Rows.Count).End(xlUp).Row
Set RegExp = CreateObject("vbscript.regexp")
RegExp.Pattern = "\d+\w+(\s)?(\s)?(\w+)?-?(\d+)?(\*\d+[包袋]?)?(\*\d+[包袋]?)?"
RegExp.Global = True
Cells(i, 4) = LCase(RegExp.Execute(Cells(i, 2))(0))
Cells(i, 5) = LCase(RegExp.Replace(Cells(i, 2), "$1") & RegExp.Execute(Cells(i, 2))(0))
Next i
End Sub
=LOWER(REPLACEB(B2,SEARCHB("?",B2),2*LEN(B2)-LENB(B2),"")&MIDB(B2,SEARCHB("?",B2),2*LEN(B2)-LENB(B2))) 非常感谢大佬,