RegExp

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:

[TABLE="class: grid, width: 750"]
<tbody>[TR]
[TD][/TD]
[TD]A[/TD]
[TD]B[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]String:[/TD]
[TD]my test 1: 123 string 23: 456 that I need to extract numbers 25 789 from.[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]Expected Output:[/TD]
[TD]123456789[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]Logic:[/TD]
[TD]Extract only where there are 3 consecutive numbers (no more, no less)[/TD]
[/TR]
</tbody>[/TABLE]

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

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
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,223,889
Messages
6,175,226
Members
452,620
Latest member
dsubash

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