Question: 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!

Answer: 
That's not solvable with an RE on its own.

Instead you can simply walk along the string tracking the longest consecutive digit group:

```
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 

```

(first result wins a tie)
If the two examples you gave, are of a standard where:

1. `<long_number>-<some_other_data>-<short_number>`
2. `<text><long_number>_<short_number>`

Are the two formats that the strings come in, there are some solutions.

However, if you are searching any string in any format for the longest number, these will not work.

### Solution 1

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

```

See the [demo](https://regex101.com/r/r6LDxq/2)

In the first capture group, you should have the longest number for those 2 formats.

**Note**: This assumes that the longest number will be the first number it encounters with an underscore or a hyphen next to it, matching those two examples given.

### Solution 2

```
\d{6,}

```

See the [demo](https://regex101.com/r/r6LDxq/3)

**Note**: This assumes that the shortest number will never exceed 5 characters in length, and the longest number will never be shorter than 6 characters in length