Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract largest numeric sequence from string (regex, or?)

Tags:

regex

vba

vb6

I have strings similar to the following:

 4123499-TESCO45-123
 every99999994_54

And I want to extract the largest numeric sequence in each string, respectively:

 4123499
 99999994

I have previously tried regex (I am using VB6)

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

 StringText = rx.Replace(StringText, "")

Which gets me partway there, but it only removes the non-numeric values, and I end up with the first string looking like:

412349945123

Can I find a regex that will give me what I require, or will I have to try another method? Essentially, my pattern would have to be anything that isn't the longest numeric sequence. But I'm not actually sure if that is even a reasonable pattern. Could anyone with a better handle of regex tell me if I am going down a rabbit hole? I appreciate any help!

like image 773
AliceSmith Avatar asked Mar 06 '23 15:03

AliceSmith


1 Answers

You cannot get the result by just a regex. You will have to extract all numeric chunks and get the longest one using other programming means.

Here is an example:

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

The \d+ with RegExp.Global=True will find all digit chunks and then only the longest will be printed after all matches are processed in a loop.

like image 153
Wiktor Stribiżew Avatar answered Mar 10 '23 11:03

Wiktor Stribiżew