VBA 包含逗号、破折号和字母数字字符的正则表达式
VBA regular expression containing commas, dashes and alphanumeric characters
我对 VBA 中包含以下字符的正则表达式有点费劲。我想要逗号、破折号和字母数字字符。
我已经弄清楚了字母数字字符,但破折号和逗号似乎给我带来了麻烦。
这是我创建的函数
Private Function StripSpecialCharacters(ByVal strField As String) As String
Dim ObjRegex As Object
Set ObjRegex = CreateObject("vbscript.regexp")
With ObjRegex
.Global = True
.Pattern = "[^a-zA-Z0-9]+" 'regular expression needs to contain commas and dashes
StripSpecialCharacters = .Replace(Replace(strField, "-", Chr(32)), vbNullString)
End With
End Function
谁能帮帮我?
更新:
好吧,我一直在倒退思考。对于那个很抱歉。
这是我从 strField 中得到的
Consult Hond's + Huid & Darm-Allergieëndieet Mix3.5 * 4 kg 8713112002917
我需要能够从此字符串中去除所有特殊的、奇怪的字符,例如 +、ë、*、。 , ', 等等。一些特殊字符需要用破折号代替。这些是以下内容;空格、+、/、&。
起初我想我要替换特殊字符然后使用我的正则表达式。但是任何改进都更受欢迎。所以拿我刚刚给的字符串。最后的结果应该是这样的。
consult-honds-huid-darm-allergiedieet-mix3,5x4kg8713112002917
(如您所见,点已被逗号替换)。
按照以下步骤操作:
输入Consult Hond's + Huid & Darm-Allergieëndieet Mix3.5 * 4 kg 8713112002917
--------------------------|-------------------------
| Find | Replace
--------------------------|-------------------------
1 | [\s\x7f-\xff'+/&]+ | nothing
2 | [.]+ | ,
3 | [*]+ | x
4 | (?<=[a-z])(?=[A-Z]) | -
--------------------------|-------------------------
5 Conveert string to lower case
输出consult-honds-huid-darm-allergiedieet-mix3,5x4kg8713112002917
我对 VBA 中包含以下字符的正则表达式有点费劲。我想要逗号、破折号和字母数字字符。
我已经弄清楚了字母数字字符,但破折号和逗号似乎给我带来了麻烦。
这是我创建的函数
Private Function StripSpecialCharacters(ByVal strField As String) As String
Dim ObjRegex As Object
Set ObjRegex = CreateObject("vbscript.regexp")
With ObjRegex
.Global = True
.Pattern = "[^a-zA-Z0-9]+" 'regular expression needs to contain commas and dashes
StripSpecialCharacters = .Replace(Replace(strField, "-", Chr(32)), vbNullString)
End With
End Function
谁能帮帮我?
更新: 好吧,我一直在倒退思考。对于那个很抱歉。
这是我从 strField 中得到的
Consult Hond's + Huid & Darm-Allergieëndieet Mix3.5 * 4 kg 8713112002917
我需要能够从此字符串中去除所有特殊的、奇怪的字符,例如 +、ë、*、。 , ', 等等。一些特殊字符需要用破折号代替。这些是以下内容;空格、+、/、&。 起初我想我要替换特殊字符然后使用我的正则表达式。但是任何改进都更受欢迎。所以拿我刚刚给的字符串。最后的结果应该是这样的。
consult-honds-huid-darm-allergiedieet-mix3,5x4kg8713112002917
(如您所见,点已被逗号替换)。
按照以下步骤操作:
输入Consult Hond's + Huid & Darm-Allergieëndieet Mix3.5 * 4 kg 8713112002917
--------------------------|-------------------------
| Find | Replace
--------------------------|-------------------------
1 | [\s\x7f-\xff'+/&]+ | nothing
2 | [.]+ | ,
3 | [*]+ | x
4 | (?<=[a-z])(?=[A-Z]) | -
--------------------------|-------------------------
5 Conveert string to lower case
输出consult-honds-huid-darm-allergiedieet-mix3,5x4kg8713112002917