如何在VBA中使用RegEx隔离空格(\ s与\ p {Zs})?

时间:2015-02-19 21:50:03

标签: regex excel-vba unicode-string vba excel

简介/问题:

我一直在研究使用正则表达式(使用VBA / Excel),到目前为止我无法理解如何使用其他空白字符中的regexp来隔离<space>(或" "\s中包含的内容。我以为我可以使用\p{Zs},但到目前为止我的测试中还没有成功。有人可以纠正我的误解吗?我感谢任何有用的意见。

要提供适当的信用,我修改了一些代码,这些代码最初是@Portland Runner的一篇非常有用的帖子,可在此处找到:How to use Regular Expressions (Regex) in Microsoft Excel both in-cell and loops

到目前为止,这是我的方法/研究:

使用字符串"14z-16z Flavored Peanuts",我一直在尝试编写一个删除"14z-16z "的RegExp,只留下"Flavored Peanuts"。我最初使用^[0-9](\S)+作为strPattern,使用以下代码片段的子程序:

Sub REGEXP_TEST_SPACE()

Dim strPattern As String
Dim strReplace As String
Dim strInput As String
Dim regEx As New RegExp

strInput = "14z-16z Flavored Peanuts"
strPattern = "^[0-9](\S)+"
strReplace = ""

With regEx
    .Global = True
    .MultiLine = True
    .IgnoreCase = True
    .pattern = strPattern
End With

If regEx.Test(strInput) Then
    Range("A1").Value = regEx.Replace(strInput, strReplace)
End If

End Sub

此方法为我提供了" Flavored Peanuts" 的A1值(请注意该字符串中的前导<space>

然后我更改了strPattern = "^[0-9](\S)+(\s)"(添加了(\s)),这给了我所需的A1值"Flavored Peanuts"。大!!!我得到了所需的输出!

但据我了解,\s代表所有空白字符,等于[ \f\n\r\t\v]。在这种情况下,我知道角色只是一个普通的单一空间 - 我不需要回车,水平标签等等。所以我试着看看我是否可以只隔离<space>字符在正则表达式(unicode分隔符:空格)中,我认为是\p{Zs}(例如,strPattern = "^[0-9](\S)+(\p{Zs})")。但是,使用这种模式并不会返回任何匹配,永远不会删除前导空格。我也尝试了更通用的\p{Z}(所有unicode分隔符),但这也没有用。

显然,我在研究中遗漏了一些东西。帮助既是理想的,也是赞赏的。谢谢。

3 个答案:

答案 0 :(得分:2)

您可以在RegEx模式中明确包含空格。以下模式可以正常使用

strPattern = "^[0-9](\S)+ "

答案 1 :(得分:2)

由于您正在尝试查找与\p{Zs} Unicode类别类的对应关系,因此您可能还希望处理所有硬空间。这段代码会很有帮助:

strPattern = "^[0-9](\S)+[ " & ChrW(160) & "]"

或者,

strPattern = "^[0-9](\S+)[ \x0A]"

[ \x0A]字符类将匹配常规空间或坚硬,不间断的空间

如果您需要匹配所有类型的空格,您可以根据https://www.cs.tut.fi/~jkorpela/chars/spaces.html上的信息使用此正则表达式模式:

strPattern = "^[0-9](\S)+[ \xA0\u1680\u180E\u2000-\u200B\u202F\u205F\u3000\uFEFF]"

这是包含代码点解释的表格:

U+0020  32  SPACE   foo bar Depends on font, typically 1/4 em, often adjusted
U+00A0  160 NO-BREAK SPACE  foo bar As a space, but often not adjusted
U+1680  5760    OGHAM SPACE MARK    foo bar Unspecified; usually not really a space but a dash
U+180E  6158    MONGOLIAN VOWEL SEPARATOR   foo᠎bar No width
U+2000  8192    EN QUAD foo bar 1 en (= 1/2 em)
U+2001  8193    EM QUAD foo bar 1 em (nominally, the height of the font)
U+2002  8194    EN SPACE    foo bar 1 en (= 1/2 em)
U+2003  8195    EM SPACE    foo bar 1 em
U+2004  8196    THREE-PER-EM SPACE  foo bar 1/3 em
U+2005  8197    FOUR-PER-EM SPACE   foo bar 1/4 em
U+2006  8198    SIX-PER-EM SPACE    foo bar 1/6 em
U+2007  8199    FIGURE SPACE    foo bar “Tabular width”, the width of digits
U+2008  8200    PUNCTUATION SPACE   foo bar The width of a period “.”
U+2009  8201    THIN SPACE  foo bar 1/5 em (or sometimes 1/6 em)
U+200A  8202    HAIR SPACE  foo bar Narrower than THIN SPACE
U+200B  8203    ZERO WIDTH SPACE    foo​bar Nominally no width, but may expand
U+202F  8239    NARROW NO-BREAK SPACE   foo bar Narrower than NO-BREAK SPACE (or SPACE)
U+205F  8287    MEDIUM MATHEMATICAL SPACE   foo bar 4/18 em
U+3000  12288   IDEOGRAPHIC SPACE   foo bar The width of ideographic (CJK) characters.
U+FEFF  65279   ZERO WIDTH NO-BREAK SPACE

最好的问候。

答案 2 :(得分:1)

只需使用文字空格字符:strPattern = "^[0-9](\S)+ "