Mar*_*erg 5 regex excel vba trim
我已经检查了很多建议,重新修改vba中的前导和尾随空格(excel,顺便说一句).
我已经找到了这个解决方案,但它也修剪了(äö(也有帽子)而且我在正则表达式上太弱了以至于不知道为什么:
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
Run Code Online (Sandbox Code Playgroud)
(这是来自SO的答案,其中useraccount似乎无效:
所以,我很乐意,如果有人可以帮助(a)问题的替代解决方案,或(b)不会剥离(单个)åäö字符的正则表达式/代码版本.
谢谢你的帮助!
细节:问题
我的上下文是vba中的xmlparser,它获取要解析的xml块.它有时只是从流中获取一个角色,可能是åäö,然后这个功能完全消失了.
当然,我很乐意澄清或编辑这个问题.
仅供参考:我根据答案分享了我的所作所为,见下文.
对于正则表达式,我将使用:
^[\s\xA0]+|[\s\xA0]+$
Run Code Online (Sandbox Code Playgroud)
这将与HTML文档中常见的“常规”空格字符以及NBSP相匹配。
VBA代码如下所示,其中S是Trim的行:
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, "")
Run Code Online (Sandbox Code Playgroud)
正则表达式的解释:
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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8722 次 |
| 最近记录: |