+ Reply to Thread
Results 1 to 4 of 4

Regular Expression Pattern

Hybrid View

goss Regular Expression Pattern 08-16-2013, 09:09 AM
Ron Coderre Re: Regular Expression Pattern 08-16-2013, 09:19 AM
JosephP Re: Regular Expression Pattern 08-16-2013, 09:19 AM
goss Re: Regular Expression Pattern 08-16-2013, 11:36 AM
  1. #1
    Forum Contributor
    Join Date
    01-07-2004
    Posts
    314

    Regular Expression Pattern

    Hi all,

    Using Excel 2010, 2013.

    What would be the correct regular expression pattern to extract this substring : 234-456-00000000-45-234 from a string like
    mnasdf 234 234-456-00000000-45-234 sdf 123 34
    The string will vary, the substring does not vary other than the digits can be any number 0-9
    I need to retain the hyphens "-" as well

    I tried patterns like
    .Pattern = "[0-9][0-9][0-9]-[0-9][0-9][0-9]-[0-9]-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9]"
    .Pattern = "/d{3}-/d{3}-/d{8}-/d{2}-/d{3}"
    But not working

    Thx
    w

    Sub Test()
        Const strTest As String = "234-456-00000000-45-234"
        Debug.Print RE6(strTest)
    End Sub
     
    Function RE6(strData As String) As String
        Dim RE As Object, REMatches As Object
         
        Set RE = CreateObject("vbscript.regexp")
        With RE
            .MultiLine = False
            .Global = False
            .IgnoreCase = True
            .Pattern = "[0-9][0-9][0-9]-[0-9][0-9][0-9]-[0-9]-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9]"
        End With
         
        Set REMatches = RE.Execute(strData)
        RE6 = REMatches(0)
         
    End Function
    Kind regards,
    w

    http://dataprose.org

  2. #2
    Forum Expert Ron Coderre's Avatar
    Join Date
    03-22-2005
    Location
    Boston, Massachusetts
    MS-Off Ver
    2013, 2016, O365
    Posts
    6,996

    Re: Regular Expression Pattern

    EDITED TO USE JosephP's pattern (which is more concise)
    Try this
    Function FindPattern(ByVal text As String) As String
    
    Dim result As String
    Dim allMatches As Object
    Dim RE As Object
    result = "no match"
    
    Set RE = CreateObject("vbscript.regexp")
    
    ' RE.Pattern = "[0-9]{3}-[0-9]{3}-[0-9]{8}-[0-9]{2}-[0-9]{3}" <-Commented out
    RE.Pattern = "\d{3}-\d{3}-\d{8}-\d{2}-\d{3}"
    RE.Global = True
    RE.IgnoreCase = True
    Set allMatches = RE.Execute(text)
    
    If allMatches.Count <> 0 Then
        result = allMatches.item(0)
    Else
        Set allMatches = RE.Execute(text)
        If allMatches.Count <> 0 Then
            result = allMatches.item(0)
        End If
    End If
    
    FindPattern = result
    End Function
    Applied this way...
    A1: mnasdf 234 234-456-00000000-45-234 sdf 123 34
    B1: =findpattern(A1)

    In that example, the function returns: 234-456-00000000-45-234

    Is that something you can work with?
    Last edited by Ron Coderre; 08-16-2013 at 09:28 AM.
    Ron
    Former Microsoft MVP - Excel (2006 - 2015)
    Click here to see the Forum Rules

  3. #3
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Regular Expression Pattern

    perhaps
            .Pattern = "\d{3}-\d{3}-\d{8}-\d{2}-\d{3}"
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  4. #4
    Forum Contributor
    Join Date
    01-07-2004
    Posts
    314

    Re: Regular Expression Pattern

    Ron , JosephP -

    Thanks for your help
    I combined with some help I received earlier from shg and using these 2 functions I now have exactly what I need.

    Thanks again,
    w

    Function GetRegEx() As Object
      On Error Resume Next
      Set GetRegEx = CreateObject("VBScript.RegExp")
    End Function
    
    Function GetCleanString(oRE As Object, strLoanNumber As String) As String
        Dim reMatches As Object
    
        With oRE
            .Pattern = "\d{2}-\d{4}-\d{15}-\d{1}"
            .Global = True
            .IgnoreCase = True
            Set reMatches = .Execute(strLoanNumber)
        End With
        
        GetCleanString = reMatches(0)
    
    End Function

+ 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. Regular Expression Pattern All Letters In String
    By goss in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 08-05-2013, 12:13 AM
  2. Regular Expression Pattern May Include Decimal
    By goss in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-01-2013, 02:15 PM
  3. regular expression to extract multi instances of pattern from cell
    By starfish_001 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 07-11-2013, 07:45 PM
  4. [SOLVED] Regular Expression
    By Kvramana in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-18-2013, 05:32 AM
  5. Regular Expression Pattern Testing Add-In
    By Firefly2012 in forum The Water Cooler
    Replies: 1
    Last Post: 02-19-2012, 06:52 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