VBA 6:正则表达式无法识别复杂的字符串 [英] VBA 6 : regex not recognizing complicated string
问题描述
我有这个字符串"1X214X942,0SX"
,其中每个X代表一个ASCII码为160的不间断空格"(确切地说是一个空格),而S代表一个空格字符.
I have this string "1X214X942,0SX"
where each X represents a "non-breaking space" (a whitespace to be exact) with the ASCII code 160, and S represents a space character.
我正试图通过此正则表达式来识别它:
I am trying to recognize it with this regex:
(\d{1,3}\s(\d{3}\s)*\d{3}(\,\d{1,3})?|\d{1,3}\,\d{1,3})
但是它不起作用,因为无法识别此空格,并且表达式仅捕获942,0
.
but it doesn't work, as this whitespace is not recognized and the expression only catches 942,0
.
我尝试查看RegExr是否可以捕获整个字符串,并且可以(( http://gskinner. com/RegExr/?2v8ic ),那么我的VBA出了点问题?
I have tried to see if RegExr can catch the whole string and it can, ( http://gskinner.com/RegExr/?2v8ic) so there's something wrong with my VBA then ?
请告知!
这是我的代码:
Sub ChangeNumberFromFRformatToENformat()
Dim SectionText As String
Dim RegEx As Object, RegC As Object, RegM As Object
Dim i As Integer
Set RegEx = CreateObject("vbscript.regexp")
With RegEx
.Global = True
.MultiLine = False
.Pattern = "(\d{1,3}\s(\d{3}\s)*\d{3}(\,\d{1,3})?|\d{1,3}\,\d{1,3})"
End With
For i = 1 To ActiveDocument.Sections.Count()
SectionText = ActiveDocument.Sections(i).Range.Text
If RegEx.test(SectionText) Then
Set RegC = RegEx.Execute(SectionText)
For Each RegM In RegC
Call ChangeThousandAndDecimalSeparator(RegM.Value)
Next 'For Each RegM In RegC
Set RegC = Nothing
Set RegM = Nothing
End If
Next 'For i = 6 To ActiveDocument.Sections.Count()
Set RegEx = Nothing
End Sub
推荐答案
序列\s
与不间断空格不匹配,但是可以使用一些方法来实现.您要考虑在正则表达式中使用的原子是\xnn
或\nnn
-这些原子通过十六进制值或八进制值与字符匹配.
The sequence \s
doesn't match the non-breaking space, however there are ways you can do it. The atoms you want to consider using in the regexp are \xnn
or \nnn
-- these match a character by it's hexadecimal value or it's octal value.
要匹配ASCII 160的不间断空格,请指定\xA0
或\240
之一,而不是\s
.
Thus to match a non-breaking space of ASCII 160, specify one of \xA0
or \240
instead of \s
.
这篇关于VBA 6:正则表达式无法识别复杂的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!