如何从vba中的字符串中提取数字组 [英] How to extract groups of numbers from a string in vba
问题描述
我有一个以下形状的字符串:
RRP 90 AVE DE GAULLE 92800 PUTEAUX 0109781431-0149012126
数字可能被其他字符分隔,而不是连字符(例如空格)。我知道如何用len()来区分它们。
我需要每个字符串的数字单独存储(例如在数组中),以便我可以区分他们用len()然后使用它们。
我发现如何将字符从字符串中删除:
如何从字符串中查找数字?
但是它不适合我的问题...
你能指引一个可以帮助我的功能或一些代码吗?
解决方案这将比循环运行得快得多
公共函数NumericOnly(s As String)As String
Dim s2 As String
Dim replace_hyphen As String
replace_hyphen =
静态re为RegExp
如果re为没有设置re =新的RegExp
re.IgnoreCase = True
re.Global = True
re.Pat t=[^ 0-9 - ]'包含空格,如果要排除空格[^ 0-9]
s2 = re.Replace(s,vbNullString)
re.Pattern =[^ 0-9]
NumericOnly = re.Replace(s2,replace_hyphen)
结束函数
I have a string of the following shape:
RRP 90 AVE DE GAULLE 92800 PUTEAUX 0109781431-0149012126
The numbers might be seperated by other chars than hyphens (eg spaces). I know how to differentiate them afterwards with len().
I need every string of numbers to be stored seperately (in an array for example), so that I can discriminate them with len() and then use them.
I have found how to strip the characters away from the string :
How to find numbers from a string?
But it doesn't suit my problem...
Could you direct me to a function or bit of code that could help me with that?
解决方案 This will run much faster than looping
Public Function NumericOnly(s As String) As String
Dim s2 As String
Dim replace_hyphen As String
replace_hyphen = " "
Static re As RegExp
If re Is Nothing Then Set re = New RegExp
re.IgnoreCase = True
re.Global = True
re.Pattern = "[^0-9 -]" 'includes space, if you want to exclude space "[^0-9]"
s2 = re.Replace(s, vbNullString)
re.Pattern = "[^0-9 ]"
NumericOnly = re.Replace(s2, replace_hyphen)
End Function
这篇关于如何从vba中的字符串中提取数字组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!