Extract the largest number sequence from a string (regular expression or?)

I have lines similar to the following:

 4123499-TESCO45-123
 every99999994_54

And I want to extract the largest numerical sequence on each line, respectively:

 4123499
 99999994

I have already tried regex (I am using VB6)

 Set rx = New RegExp
 rx.Pattern = "[^\d]"
 rx.Global = True

 StringText = rx.Replace(StringText, "")

Which gets me partially there, but it only removes non-numeric values, and I get the first line that looks like this:

412349945123

Can I find a regex that will give me what I need, or will I have to try another method? Essentially, my pattern should be what is not the longest numerical sequence. But I'm not sure if this is even a reasonable model. Can someone with a better regex handle tell me if I'm going down a rabbit hole? I appreciate any help!

+4
4

. , .

:

Dim strPattern As String: strPattern = "\d+"
Dim str As String: str = "4123499-TESCO45-123"
Dim regEx As New RegExp
Dim matches  As MatchCollection
Dim match As Match
Dim result As String

With regEx
     .Global = True
     .MultiLine = False
     .IgnoreCase = False
     .Pattern = strPattern
End With

Set matches = regEx.Execute(str)
For Each m In matches
  If result < Len(m.Value) Then result = m.Value
Next

Debug.Print result

\d+ RegExp.Global=True , , , .

+3

RE .

, :

For i = 1 To Len(StringText)
    If IsNumeric(Mid$(StringText, i, 1)) Then
        a = a & Mid$(StringText, i, 1)
    Else
        a = ""
    End If
    If Len(a) > Len(longest) Then longest = a
Next

MsgBox longest 

( )

+2

, , , :

  • <long_number>-<some_other_data>-<short_number>
  • <text><long_number>_<short_number>

, , .

, , .

1

([0-9]+)[_-].*

.

. , , , .

2

\d{6,}

. , 5 , 6

+1

, .
VB. .
.
, - . Superspeed. ~ 30 , regexp:)
.
, .

, ,
.

Public Sub sb_BigNmb()
Dim sSrc$, sTgt$
Dim taSrc() As Byte, taTgt() As Byte, tLB As Byte, tUB As Byte
Dim s As Byte, t As Byte, tLenMin As Byte

    tLenMin = 4
    sSrc = "every99999994_54"

    sTgt = vbNullString

    taSrc = StrConv(sSrc, vbFromUnicode)
    tLB = LBound(taSrc)
    tUB = UBound(taSrc)

    ReDim taTgt(tLB To tUB)

    t = 0
    For s = tLB To tUB
        Select Case taSrc(s)
            Case 48 To 57
                taTgt(t) = taSrc(s)
                t = t + 1
            Case Else
                If CBool(t) Then Exit For   ' *** EXIT FOR ***
        End Select
    Next

    If (t > tLenMin) Then
        ReDim Preserve taTgt(tLB To (t - 1))
        sTgt = StrConv(taTgt, vbUnicode)
    End If

    Debug.Print "'" & sTgt & "'"
    Stop

End Sub

sSrc = "ev_1_ery99999994_54", , :) .

+1

Source: https://habr.com/ru/post/1695055/


All Articles