+ Reply to Thread
Results 1 to 9 of 9

Extracting substrings using regex

Hybrid View

  1. #1
    Registered User
    Join Date
    07-19-2018
    Location
    harare
    MS-Off Ver
    2013
    Posts
    20

    Extracting substrings using regex

    Hi,

    I have written this code to loop over a range with strings to extract a certain code in each string, which will be in CAPS and followed by 1 or 2 digits using regex. The problem is it is quite slow for the range I am working with. So I figured my code might be inefficient. May you kindly assist in ways I can speed it up. I have attached a sample.
    Thanks
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool Hi ! Try this …


    On an old laptop computer your code does not seems slow on your attachment …

    Anyway a VBA basics demonstration as a beginner starter :

    PHP Code: 
    Sub Demo1()
            
    Dim VR&
        
    With Sheet1.Range("B3"Sheet1.[C2].End(xlDown)).Columns
            V 
    = .Item(2).Value2
            
    For 1 To UBound(V):  V(R1) = Split(Split(V(R1))(4), "_")(0):  Next
           
    .Item(1).Value2 V
        End With
    End Sub 
    ► Do you like it ? ► So thanks to click on bottom left star icon « Add Reputation » !
    Last edited by Marc L; 06-06-2021 at 11:05 AM. Reason: typo …

  3. #3
    Registered User
    Join Date
    07-19-2018
    Location
    harare
    MS-Off Ver
    2013
    Posts
    20

    Re: Extracting substrings using regex

    Thanks so much, Marc. Yes, its quite fast on this small range. But comparing the run time with the code you provided(0.34 sec runtime) vs mine which is like 1.5 sec.
    In my sample strings, I forgot to put some where they don't have an underscore after the key substring we want, how can I solve this

  4. #4
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,420

    Re: Extracting substrings using regex

    Quote Originally Posted by gringol View Post
    In my sample strings, I forgot to put some where they don't have an underscore after the key substring we want...
    What do they have there... a space? a dot? a dash? something else?

    Better yet, why not post a new example with more complete representative values?

  5. #5
    Registered User
    Join Date
    07-19-2018
    Location
    harare
    MS-Off Ver
    2013
    Posts
    20

    Re: Extracting substrings using regex

    Thanks I have added a new workbook with new strings
    Attached Files Attached Files

  6. #6
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,420

    Re: Extracting substrings using regex

    Quote Originally Posted by gringol View Post
    Thanks I have added a new workbook with new strings
    Why is GHUIKDHY67 the correct value to return from this...

    C:\Users\Dell Precision 5540\Documents\1\20210525 - GHUIKDHY6721Q2.xlsm

    and not GHUIKDHY6721 instead?

  7. #7
    Registered User
    Join Date
    07-19-2018
    Location
    harare
    MS-Off Ver
    2013
    Posts
    20

    Re: Extracting substrings using regex

    Quote Originally Posted by Rick Rothstein View Post
    Why is GHUIKDHY67 the correct value to return from this...

    C:\Users\Dell Precision 5540\Documents\1\20210525 - GHUIKDHY6721Q2.xlsm

    and not GHUIKDHY6721 instead?
    Yes it was as the regex pattern had [0-9]{0,2} to extract a max of 2 digits after the Capital letters substring

  8. #8
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,611

    Re: Extracting substrings using regex

    Prepare the output array variable, b in the code and dump the result at one time.
    e.g
    Sub cODEeXTR()
    
    Dim PathList() As Variant, b
    Dim Path As Variant
    Dim str As Variant
    Dim regEx As New RegExp
    Dim item As Variant, Codes As MatchCollection
    
    StartTime = Timer
    Application.ScreenUpdating = False
    regEx.Pattern = "[A-Z]{3,}[0-9]{0,2}"
    With Sheets("sheet1")
        With .Range("c2", .Range("c" & Rows.Count).End(xlUp))
            PathList = .Value
            ReDim b(1 To UBound(PathList, 1), 1 To 1)
            For i = 1 To UBound(PathList, 1)
                Set Codes = regEx.Execute(PathList(i, 1))
                For Each item In Codes
                    b(i, 1) = item
                Next
            Next
            .Columns(0).Value = b
        End With
    End With
     SecondsElapsed = Format(Timer - StartTime, "0.0000")
    
    'Notify user in seconds
      MsgBox "This code ran successfully in " & SecondsElapsed & " seconds", vbInformation
    Application.ScreenUpdating = True
    
    End Sub

  9. #9
    Registered User
    Join Date
    07-19-2018
    Location
    harare
    MS-Off Ver
    2013
    Posts
    20

    Re: Extracting substrings using regex

    Thanks so much guys for the help!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Color substrings if between 2 substrings
    By Jonathan78 in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 03-17-2021, 04:10 PM
  2. [SOLVED] Extracting up to 3 substrings from a string
    By darls15 in forum Excel Programming / VBA / Macros
    Replies: 26
    Last Post: 07-26-2020, 06:40 PM
  3. [SOLVED] Instr with RegEx - Runtime error 5017 (RegEx Code provided)
    By dluhut in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-28-2018, 05:02 PM
  4. Need help extracting data from web page source into excel using RegEx and macro
    By chococ in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-20-2014, 03:45 PM
  5. Creating substrings
    By VTEC in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 11-22-2013, 06:24 PM
  6. Replies: 3
    Last Post: 11-21-2012, 03:28 PM
  7. sum wrt substrings!
    By via135 in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 03-26-2006, 02:06 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1