extracting a set of numbers with regex

elefantsko

New Member
Joined
Feb 18, 2015
Messages
3
Hi! I need some help with regex.. I'm trying to find a solution to extract a set of numbers from a long text. The text might look like:
"Blablabla bla bla blablabla 2 blabla bla 44 blablabla 1234567-0100 bla bla blabla"

What I need to extract from that text is "1234567-0100" (with the - ).

This is what i got:

Code:
Sub simpleRegex()    Dim strPattern As String: strPattern = "[0-9]{7}\-[0-9]{4}"
    Dim returnString As String: returnString = ""
    Dim regEx As New RegExp
    Dim strInput As String
    Dim Myrange As Range


    Set Myrange = ThisWorkbook.Worksheets(1).Range("A1")


    If strPattern <> "" Then
        strInput = Myrange.Value


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


        If regEx.Test(strInput) Then
            MsgBox (regEx.Replace(strInput, returnString))
        Else
            MsgBox ("Not matched")
        End If
    End If
End Sub

but this returns everything before the set of numbers...

Anyone?

Regards!
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Hi,

Your pattern is fine, try this one.


Code:
Sub simpleRegex()
Dim RegExp As Object, Collection As Object, RegMatch As Object
Dim MyRange As Range, Outstring As String
    Set RegExp = CreateObject("vbscript.RegExp")
    With RegExp
        .Global = True
        .Pattern = "[0-9]{7}\-[0-9]{4}"
    End With
    Set MyRange = ThisWorkbook.Worksheets(1).Range("A1") 'change to suit
    
        Outstring = ""
        Set Collection = RegExp.Execute(MyRange.Value)
        For Each RegMatch In Collection
            Outstring = Outstring & RegMatch
        Next
        If Outstring <> "" Then
            MsgBox Outstring
        Else
            MsgBox "Not MAtched"
        End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,213,528
Messages
6,114,154
Members
448,553
Latest member
slaytonpa

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