+ Reply to Thread
Results 1 to 8 of 8

Regular Expression Pattern All Letters In String

Hybrid View

goss Regular Expression Pattern... 08-04-2013, 12:54 PM
nilem Re: Regular Expression... 08-04-2013, 01:16 PM
shg Re: Regular Expression... 08-04-2013, 01:17 PM
goss Re: Regular Expression... 08-04-2013, 02:27 PM
shg Re: Regular Expression... 08-04-2013, 02:53 PM
shg Re: Regular Expression... 08-04-2013, 02:57 PM
goss Re: Regular Expression... 08-04-2013, 08:50 PM
shg Re: Regular Expression... 08-05-2013, 12:13 AM
  1. #1
    Forum Contributor
    Join Date
    01-07-2004
    Posts
    314

    Regular Expression Pattern All Letters In String

    Hi all,

    Using Excel 2013.

    Trying to get the pattern to return all letters in a string
    My test string is "asdf134!#@$adsf]"
    My current pattern is "[a-zA-Z]*"
    So the result should be "asdfadsf"
    But my pattern stops when 1 is encountered so the output is "asdf"

    How can I get the pattern to continue through the entire string?

    thx
    w

    Sub:
    Sub foo()
    
        Dim t As String
        Dim s As String
        
        s = "asdf134!#@$adsf]"
        t = TestRE(s)
    
        Debug.Print t
    
    End Sub
    Function
    Function TestRE(s As String) As String
        
        Dim regex As New RegExp
        Dim colregmatch As MatchCollection
    
        With regex
           .Pattern = "[a-zA-Z]*"
           .MultiLine = False
           .Global = True
           .IgnoreCase = False
        End With
    
        Set colregmatch = regex.Execute(s)
        
        TestRE = colregmatch(0)
    End Function
    Kind regards,
    w

    http://dataprose.org

  2. #2
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: Regular Expression Pattern All Letters In String

    Hi goss,
    try it
    Sub foo()
    Dim t As String, s As String
    s = "asdf134!#@$ad654sU]"
    t = TestRE(s)
    MsgBox t
    End Sub
    
    Function TestRE(s As String) As String
    With CreateObject("vbscript.regexp")
        .Pattern = "[^a-z]"
        .Global = True
        .IgnoreCase = True
        TestRE = .Replace(s, "")
    End With
    End Function
    (I forgot the name link to the Regexp in the available references)
    Last edited by nilem; 08-04-2013 at 01:20 PM.

  3. #3
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Regular Expression Pattern All Letters In String

    How about ...

    Function TestRE(s As String) As String
        With New RegExp
           .Pattern = "[^A-Z]"
           .Global = True
           .IgnoreCase = True
            TestRE = .Replace(s, "")
        End With
    End Function
    Entia non sunt multiplicanda sine necessitate

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

    Re: Regular Expression Pattern All Letters In String

    Nilem / Shg,

    Thanks.
    Nice use of recursion - the function works great!

    Do you know is there a class for letters only.
    I've Google'd quite a bit haven't found anything.

    thx
    w

  5. #5
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Regular Expression Pattern All Letters In String

    There's no recursion in those functions.

    "[A-Za-z]" is the pattern for letters, or just "[A-Z]" if ignoring case.

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Regular Expression Pattern All Letters In String

    I would combine nilems's late-binding approach (to avoid the need for a reference) with a static object variable for speed:

    Function TestRE(s As String) As String
        Static oRE      As Object
    
        If oRE Is Nothing Then
            Set oRE = CreateObject("VBScript.RegExp")
            oRE.Pattern = "[^A-Z]"
            oRE.Global = True
            oRE.IgnoreCase = True
        End If
    
        TestRE = oRE.Replace(s, "")
    End Function

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

    Re: Regular Expression Pattern All Letters In String

    Thanks shg,

    Now I understand.
    I was trying to extract only letters
    You are replacing if not a letter with nothing.

    Genius, thanks!
    w

  8. #8
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Regular Expression Pattern All Letters In String

    You're welcome.

+ 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 May Include Decimal
    By goss in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-01-2013, 02:15 PM
  2. 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
  3. [SOLVED] Find a string by VBA Regular Expression and replace a part of that string
    By taps in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-25-2013, 11:08 AM
  4. Regular Expression Pattern Testing Add-In
    By Firefly2012 in forum The Water Cooler
    Replies: 1
    Last Post: 02-19-2012, 06:52 PM
  5. Using a regular expression to pull out data from a string
    By mundo in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 03-12-2008, 07:42 AM

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