我试图将数字的最后两位数字替换为“ XX BLOCK
”,如果它出现在字符串的开头并且超过2位数字。
我正在使用Microsoft VBScript正则表达式5.5参考。
Dim regEx As New RegExp
With regEx
.Global = True 'Matches whole string, not just first occurrence
.IgnoreCase = True 'Matches upper or lowercase
.MultiLine = True 'Checks each line in case cell has multiple lines
.pattern = "^(\d{2,})" 'Checks beginning of string for at least 2 digits
End With
'cell is set earlier in code not shown, refers to an Excel cell
regEx.replace(cell.Value, "XX BLOCK")
所需结果:
"1091 foo address" --> "10XX BLOCK foo address"
"1016 foo 1010 address" --> "10XX BLOCK foo 1010 address"
"foo 1081 address" --> "foo 1081 address"
"10 bar address" --> "XX BLOCK bar address"
"8 baz address" --> "8 baz address"
我是regex的新手,不确定从哪里开始。我尝试使用,^(\d{2,})
但随后替换了整个数字。
还可以保证数字(如果存在)将始终带有空格。
您可以使用
^(\d*)\d{2}\b
或者,如果您不能依靠单词边界,也可以使用
^(\d*)\d{2}(?!\d) ' no digit is allowed after the 2-digit sequence
^(\d*)\d{2}(?!\S) ' a whitespace boundary
并替换为$1XX BLOCK
。
请参阅regex演示。
细节
^
-字符串开始(\d*)
-第1组:零个或多个数字\d{2}
-两位数\b
-字边界,无数字,字母或_
在两个数字之后(?!\d)
-如果当前位置的右侧紧邻有一个数字,则负向预测将使匹配失败(?!\S)
-如果当前位置的右侧紧邻有一个非空白字符,则负匹配将使匹配失败。本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句