我需要帮助在Access 2010中创建一个VB代码或表达式,它将从一个字符串中对数字进行分组,其中每个字符串以数字6开头并且总是9个字符长。
字符串示例:
订单确认#638917872-001部分订单/收到23.74美元
订单确认 - 多个订单 - 订单确认#639069135-001 / $ 297.45 - 订单确认#639069611-001 / $ 32.08。
我正在使用VB代码删除所有字母字符,但这只留给我:
6389178720012374
来自字符串2的639069135001297456390696110013208
。我所关心的是以6开头的订单号,长度为9个字符。任何帮助将不胜感激,我知道有一个更简单的方法。
答案 0 :(得分:2)
VB.NET解决方案:
如果您只需要生成的字符串中的前9个数字,则可以使用String.Substring,即:
Dim numberString as String = "6389178720012374"
Dim newString As String = numberString.Substring(0, 9)
MessageBox.Show(newString)
显示638917872
编辑:
也许你会想要使用RegEx - 这样的事情可能会让你开始:
Private Sub Input()
Dim numberString As String = "Order Confirmation # 638917872-001 Partial Order/$23.74 RECEIVED"
Dim numberString2 As String = "Order Confirmation - Multiple Orders - Order Confirmation#639069135-001/$297.45 - Order Confirmation#639069611-001/$32.08"
GiveMeTheNumbers(numberString)
GiveMeTheNumbers(numberString2)
End Sub
Function GiveMeTheNumbers(ByVal s As String) As String
Dim m As Match = Regex.Match(s, "6\d{8}") 'get 9 digit #s begin w/6
Do While m.Success
MessageBox.Show(m.Value.ToString)
m = m.NextMatch()
Loop
Return False
End Function
结果 - MessageBox1:638917872 MessageBox2:639069135 MessageBox3:639069611
答案 1 :(得分:0)
您可以使用此功能...在VB.NET中测试
Function NumOnly(ByVal s As String) As String
sRes = ""
For x As Integer = 0 To s.Length - 1
If IsNumeric(s.Substring(x, 1)) Then sRes = sRes & s.Substring(x, 1)
Next
return sRes
End Function
ms-access的小修改
答案 2 :(得分:0)
好的,这是VBA解决方案。您需要在参考文献中添加 Microsoft VBScript正则表达式 这将匹配它找到的每个9位数字,并返回一个字符串数组,其顺序为#s。
Function GetOrderNum(S As String) As String()
Dim oMatches As Object
Dim aMatches() As String
Dim I As Integer
Dim RE As Object
Set RE = CreateObject("vbscript.regexp")
ReDim aMatches(0)
RE.Pattern = "\d{9}"
RE.Global = True
RE.IgnoreCase = True
Set oMatches = RE.Execute(S)
If oMatches.Count <> 0 Then
ReDim aMatches(oMatches.Count)
For I = 0 To oMatches.Count - 1
aMatches(I) = oMatches(I)
Next I
End If
GetOrderNum = aMatches
End Function