tyija1995

Well-known Member
Joined
Feb 26, 2019
Messages
781
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I've tried reading up on regular expressions but I am drawing a blank as to how to extract from a string 3 consecutive digits (no more, no less).

So if I have:

AB
1String:my test 1: 123 string 23: 456 that I need to extract numbers 25 789 from.
2Expected Output:123456789
3Logic:Extract only where there are 3 consecutive numbers (no more, no less)

<tbody>
</tbody>

I have wrote this but it's doing the opposite and I can't figure out how to negate it...

Code:
Function Extract3Digits(txt As String) As String
    Dim rgx As Object
    Set rgx = CreateObject("VBScript.RegExp")
        With rgx
            .Global = True
            .Pattern = "\d{3}"
            Extract3Digits = .Replace(txt, "")
        End With
End Function

Used on my string provided I get:
"my test 1: string 23: that I need to extract numbers 25 from."
So the bit I have replaced essentially is the bit I want to reference, but I don't know how to.

Thanks!
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Re: RegExp Help

Apologies - found an answer now :)!
 
Upvote 0
Re: RegExp Help

You can also do this without using Regular Expressions...
Code:
Function Extract3Digits(ByVal S As String) As String
  Dim X As Long, Arr As Variant
  For X = 1 To Len(S)
    If Mid(S, X, 1) Like "[!0-9]" Then Mid(S, X) = " "
  Next
  Arr = Split(Application.Trim(S))
  For X = 0 To UBound(Arr)
    If Len(Arr(X)) <> 3 Then Arr(X) = ""
  Next
  Extract3Digits = Replace(Join(Arr), " ", "")
End Function
 
Upvote 0
Re: RegExp Help

Hi Rick,

Thanks for this other solution - very good! Just saw your reply on the other post with this too, have made a note of this and will use it going forward should I need it, many thanks! :)
 
Upvote 0

Forum statistics

Threads
1,214,429
Messages
6,119,424
Members
448,896
Latest member
MadMarty

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