RegEx: first number after a certain word

Nelson78

Well-known Member
Joined
Sep 11, 2017
Messages
521
Office Version
  1. 2007
Hello everybody.
I have strings like these:

VBA Code:
London1725
LondonMadrid
LondonMadrid2528
London5896Madrid7852

I need: first number after the string London but not after Madrid

So, wished results for each of the four strings above:

VBA Code:
1725


5896

So, this is the starting point:

VBA Code:
(\d+)

How can I develop the code?

Thank you in advance.
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Here is the expression

London(\d+)

result is $1

Do you just need the RegEx expression, or the complete VBA code?
 
Upvote 0
VBA Code:
Function jec(cell As String) As Double
 With CreateObject("VBScript.RegExp")
   .Global = True
   .Pattern = "London(\d+)(.*)"
   jec = .Replace(cell, "$1")
 End With
End Function
 
Upvote 0
I've built this way, but something's wrong:

VBA Code:
Sub extract()

Dim lr As Long, fnd As Object, itm As Object

lr = Worksheets(1).Cells(Rows.Count, "A").End(xlUp).Row

    With CreateObject("VBScript.RegExp")
        .Global = True
        .Pattern = "London(\d+)(.*)"
            For Each cell In Sheets(1).Range("A2:A" & lr)
                  Set fnd = .Execute(cell.Value)
                      For Each itm In fnd
                            cell.Offset(0, 1) = itm
                            Exit For
                      Next itm
            Next cell

    End With

Results:

startresult
London1725London1725
LondonMadrid
LondonMadrid2528
London5896Madrid7852London5896Madrid7852

I expect in B2 "1725" instead "London1725" and in B5 "5896" instead of "London5896Madrid7852".
 
Upvote 0
You are not using the method I provided
 
Upvote 0

Forum statistics

Threads
1,203,382
Messages
6,055,104
Members
444,763
Latest member
Jaapaap

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top