如何检查一个string是否包含一个连续的整数

我有一些信息,只需要从中提取整数。

例如:

A1 : 130001 A2 : hello_24.02_75_150001 A3 : 650000_take:away A4 : computer_800000_24.01.105 

我只想拿六位数字(130000,150001,650000,800000)。 我怎样才能得到这些?

我试过IsNumeric(number)

 If Regex.IsMatch(number, "^[0-9 ]+$") Then ... End If 

您可以在VBA中使用用户定义的公式。 将下面的代码放在一个新的模块中,并将其作为Excel中的函数调用=Extract6Digits(A1)

 Function Extract6Digits(Number As String) Dim varCount As Integer Dim i As Integer Dim varOutput As String Dim varTemp As String varCount = 0 varTemp = "" For i = 1 To Len("" & Number & "") If Asc(Mid("" & Number & "", i, 1)) >= 48 And Asc(Mid("" & Number & "", i, 1)) <= 57 Then varCount = varCount + 1 varTemp = varTemp & Mid("" & Number & "", i, 1) Else varCount = 0 varTemp = "" End If If varCount = 6 Then If varOutput = "" Then varOutput = varTemp Else varOutput = varOutput & "," & varTemp End If varCount = 0 varTemp = "" End If Next Extract6Digits = varOutput End Function 

这绝不是最有效的方法,但它的工作原理

我以为你想要正则regexp解决scheme;)

这是你想要的吗?

 Sub Sample() Dim s As String Dim regEx, Match, matches Dim rngRange As Range s = "A123456X hello_24.02_75_150001 A3 : 6500000_take:away A4 : computer_800000_24.01.105 987654" With CreateObject("vbscript.regexp") .Pattern = "(^|\D)(\d{6})(\D|$)" .Global = True Set matches = .Execute(s) If matches.Count > 0 Then For Each Match In matches Debug.Print Match.SubMatches(1) '~~> Result '123456 '150001 '800000 '987654 Next End If End With End Sub 

尝试这个

 Sub test() lastrow = Range("A" & Rows.Count).End(xlUp).Row For i = 1 To lastrow totallen = Len(Range("A" & i).Value) For j = 1 To totallen thischar = Mid(Range("A" & i), j, 1) Select Case thischar Case "0", "1", "2", "3", "4", "5", "6", "7", "8", "0" If IsNumeric(Mid(Range("A" & i), j, 6)) = True Then Range("B" & i).Value = Mid(Range("A" & i), j, 6) Exit For End If End Select Next j Next i End Sub 

尝试

 Function GetSixDigits(s As String) As String Dim intIndex As Integer Dim strArr() As String strArr = Split(s, "_") For intIndex = LBound(strArr) To UBound(strArr) If Len(strArr(intIndex)) = 6 And IsNumeric(strArr(intIndex)) Then GetSixDigits = strArr(intIndex) Exit Function End If Next End Function 

在Siddharth Rout和AranDG的回答中,我做了一些性能testing。

Siddharth Rout的解决scheme的testing

 '---------------------------------------- 'adapted from Siddharth Rout's answer ' works Sub Sample() Dim regEx, Match, matches Dim rngRange As Range, cell As Range Dim i As Integer Dim mytime As Long mytime = Timer With CreateObject("vbscript.regexp") .Pattern = "(^|\D)(\d{6})(\D|$)" .Global = True For Each cell In Range("A1:A12000").SpecialCells(xlCellTypeConstants, xlTextValues) Set matches = .Execute(cell.Value) If matches.Count > 0 Then i = 0 For Each Match In matches i = i + 1 cell.Offset(, i) = Match.SubMatches(1) Next End If Next cell End With MsgBox Timer - mytime End Sub '---------------------------------------- 

AranDG解决scheme的testing:

 '---------------------------------------- 'adapted form AranDG ' works Sub Sample4() Dim cell As Range Dim arr As Variant Dim mytime As Long mytime = Timer For Each cell In Range("A1:A12000").SpecialCells(xlCellTypeConstants, xlTextValues) ' cell.Offset(, 1) = Extract6Digits(cell.Value) arr = Extract6Digits(cell.Value) cell.Offset(, 1).Resize(, UBound(arr) + 1) = arr Next cell MsgBox Timer - mytime End Sub Function Extract6Digits(Number As String) As Variant Dim varCount As Integer Dim i As Integer Dim varOutput As String Dim varTemp As String varCount = 0 varTemp = "" For i = 1 To Len("" & Number & "") If Asc(Mid("" & Number & "", i, 1)) >= 48 And Asc(Mid("" & Number & "", i, 1)) <= 57 Then varCount = varCount + 1 varTemp = varTemp & Mid("" & Number & "", i, 1) Else varCount = 0 varTemp = "" End If If varCount = 6 Then If varOutput = "" Then varOutput = varTemp Else varOutput = varOutput & "," & varTemp End If varCount = 0 varTemp = "" End If Next Extract6Digits = Split(varOutput, ",") End Function '---------------------------------------- 

而且我用以下string从“A1”填充到“A1000”的活动表格列“A”中运行它们:

 hello_24.02_75_150001 A3 : 650000_take:away A4 : computer_800000_24.01.105 hello_24.02_75_150001 A3 : 650000_take:away A4 : computer_800000_24.01.105 hello_24.02_75_150001 A3 : 650000_take:away A4 : computer_800000_24.01.105 hello_24.02_75_150001 A3 : 650000_take:away A4 : computer_800000_24.01.105 hello_24.02_75_150001 A3 : 650000_take:away A4 : computer_800000_24.01.105 hello_24.02_75_150001 A3 : 650000_take:away A4 : computer_800000_24.01.105 hello_24.02_75_150001 A3 : 650000_take:away A4 : computer_800000_24.01.105 hello_24.02_75_150001 A3 : 650000_take:away A4 : computer_800000_24.01.105 hello_24.02_75_150001 A3 : 650000_take:away A4 : computer_800000_24.01.105 hello_24.02_75_150001 A3 : 650000_take:away A4 : computer_800000_24.01.105 hello_24.02_75_150001 A3 : 650000_take:away A4 : computer_800000_24.01.105 hello_24.02_75_150001 A3 : 650000_take:away A4 : computer_800000_24.01.105 hello_24.02_75_150001 A3 : 650000_take:away A4 : computer_800000_24.01.105 hello_24.02_75_150001 A3 : 650000_take:away A4 : computer_800000_24.01.105 hello_24.02_75_150001 A3 : 650000_take:away A4 : computer_800000_24.01.105 hello_24.02_75_150001 A3 : 650000_take:away A4 : computer_800000_24.01.105 hello_24.02_75_150001 A3 : 650000_take:away A4 : computer_800000_24.01.105 hello_24.02_75_150001 A3 : 650000_take:away A4 : computer_800000_24.01.105 hello_24.02_75_150001 A3 : 650000_take:away A4 : computer_800000_24.01.105 hello_24.02_75_150001 A3 : 650000_take:away A4 : computer_800000_24.01.105 hello_24.02_75_150001 A3 : 650000_take:away A4 : computer_800000_24.01.105 hello_24.02_75_150001 A3 : 650000_take:away A4 : computer_800000_24.01.105 hello_24.02_75_150001 A3 : 650000_take:away A4 : computer_800000_24.01.105 hello_24.02_75_150001 A3 : 650000_take:away A4 : computer_800000_24.01.105 hello_24.02_75_150001 A3 : 650000_take:away A4 : computer_800000_24.01.105 hello_24.02_75_150001 A3 : 650000_take:away A4 : computer_800000_24.01.105 hello_24.02_75_150001 A3 : 650000_take:away A4 : computer_800000_24.01.105 hello_24.02_75_150001 A3 : 650000_take:away A4 : computer_800000_24.01.105 hello_24.02_75_150001 A3 : 650000_take:away A4 : computer_800000_24.01.105 hello_24.02_75_150001 A3 : 650000_take:away A4 : computer_800000_24.01.105 hello_24.02_75_150001 A3 : 650000_take:away A4 : computer_800000_24.01.105 hello_24.02_75_150001 A3 : 650000_take:away A4 : computer_800000_24.01.105 hello_24.02_75_150001 A3 : 650000_take:away A4 : computer_800000_24.01.105 hello_24.02_75_150001 A3 : 650000_take:away A4 : computer_800000_24.01.105 hello_24.02_75_150001 A3 : 650000_take:away A4 : computer_800000_24.01.105 hello_24.02_75_150001 A3 : 650000_take:away A4 : computer_800000_24.01.105 hello_24.02_75_150001 A3 : 650000_take:away A4 : computer_800000_24.01.105 hello_24.02_75_150001 A3 : 650000_take:away A4 : computer_800000_24.01.105 hello_24.02_75_150001 A3 : 650000_take:away A4 : computer_800000_24.01.105 hello_24.02_75_150001 A3 : 650000_take:away A4 : computer_800000_24.01.105