函数来修剪vba中的前导和后缀空格

我已经检查了几个build议,重新调整vba中的首尾空白(顺便说一下,excel)。

我已经find了这个解决scheme,但它也修剪åäö(也上限),我在正则expression式太弱,看看为什么:

Function MultilineTrim (Byval TextData) Dim textRegExp Set textRegExp = new regexp textRegExp.Pattern = "\s{0,}(\S{1}[\s,\S]*\S{1})\s{0,}" textRegExp.Global = False textRegExp.IgnoreCase = True textRegExp.Multiline = True If textRegExp.Test (TextData) Then MultilineTrim = textRegExp.Replace (TextData, "$1") Else MultilineTrim = "" End If End Function 

(这是来自SO的一个答案,那里的使用账号似乎不活跃:

https://stackoverflow.com/a/1606433/3701019 )

所以,如果有人能够帮助解决这个问题,或者(b)不会去掉(单个)字符的正则expression式/代码的版本,我会很乐意。

谢谢你的帮助!

详细信息:问题

  • vba中的修剪function不考虑所有的空白字符(例如制表符)。 一些自定义修剪是必要的
  • 我find的最好的解决scheme是上面,但它也删除单个字符。

我的上下文是vba中的一个xmlparser,在这里它得到了大量的xmlparsing。 它有时只是从stream中获得一个字符,可能是这个字符,然后这个函数就会完全去掉。

当然,我很乐意澄清或编辑这个问题。

供参考:我已经根据答案分享了我所做的,见下文。

对于一个正则expression式,我会使用:

 ^[\s\xA0]+|[\s\xA0]+$ 

这将匹配“常用”空白字符以及HTML文档中常见的NBSP。

VBA代码如下所示,其中S是要修剪的行:

 Dim RE as Object, ResultString as String Set RE = CreateObject("vbscript.regexp") RE.MultiLine = True RE.Global = True RE.Pattern = "^[\s\xA0]+|[\s\xA0]+$" ResultString = RE.Replace(S, "") 

和正则expression式的解释:

 Trim whitespace at the start and the end of each line ----------------------------------------------------- ^[\s\xA0]+|[\s\xA0]+$ Options: ^$ match at line breaks Match this alternative (attempting the next alternative only if this one fails) «^[\s\xA0]+» Assert position at the beginning of a line (at beginning of the string or after a line break character) «^» Match a single character present in the list below «[\s\xA0]+» Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+» A “whitespace character” (ASCII space, tab, line feed, carriage return, vertical tab, form feed) «\s» The character with position 0xA0 (160 decimal) in the character set «\xA0» Or match this alternative (the entire match attempt fails if this one fails to match) «[\s\xA0]+$» Match a single character present in the list below «[\s\xA0]+» Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+» A “whitespace character” (ASCII space, tab, line feed, carriage return, vertical tab, form feed) «\s» The character with position 0xA0 (160 decimal) in the character set «\xA0» Assert position at the end of a line (at the end of the string or before a line break character) «$» Created with RegexBuddy 

尝试这个:

 Function MultilineTrim (Byval TextData) Dim textRegExp Set textRegExp = new regexp textRegExp.Pattern = "(^[ \t]+|[ \t]+$)" textRegExp.Global = True textRegExp.IgnoreCase = True textRegExp.Multiline = True MultilineTrim = textRegExp.Replace (TextData, "") End Function 

您可以创build一个自定义函数,去除不需要的字符。

 Private Function CleanMyString(sInput As String) As String Dim sResult As String ' Remove leading ans trailing spaces sResult = Trim(sInput) 'Remove other characters that you dont want sResult = Replace(sResult, chr(10), "") sResult = Replace(sResult, chr(13), "") sResult = Replace(sResult, chr(9), "") End Function 

这虽然不使用正则expression式。 不知道这是否可以满足您的要求?

在与stackexchange人讨论如何做到这一点之后 ,我将该问题的编辑添加为我自己的答案。 这里是:

回答/使用的代码

感谢答复,这是我将使用:

 Function MultilineTrim(ByVal TextData) MultilineTrim = textRegExp.Replace(TextData, "") ' If textRegExp.Test(TextData) Then ' MultilineTrim = textRegExp.Replace(TextData, "$1") ' Else ' MultilineTrim = "" ' ?? ' End If End Function Private Sub InitRegExp() Set textRegExp = New RegExp 'textRegExp.Pattern = "\s{0,}(\S{1}[\s,\S]*\S{1})\s{0,}" 'this removes å ä ö - bug! 'textRegExp.Global = False 'textRegExp.Pattern = "(^[ \t]+|[ \t]+$)" ' leaves a line break at start textRegExp.Pattern = "^[\s\xA0]+|[\s\xA0]+$" ' works! Ron Rosenfelds submit textRegExp.Global = True textRegExp.IgnoreCase = True textRegExp.MultiLine = True End Sub 

再次感谢所有! (向Ron Rosenfeld点头)

重构和改进理查德Vivians版本

 Function cleanMyString(sInput) ' Remove leading and trailing spaces sInput = Trim(sInput) 'Remove other characters that you dont want sInput = Replace(sInput, Chr(10), "") sInput = Replace(sInput, Chr(13), "") sInput = Replace(sInput, Chr(9), "") cleanMyString = sInput End Function 

我将在更换所有其他字符后调用Trim。 这样,如果其他字符之后有空格,它们也将被删除。