Consulting

Results 1 to 7 of 7

Thread: Regular Expressions convert uppercase to lowercase

  1. #1
    VBAX Regular
    Joined
    Oct 2015
    Posts
    17
    Location

    Regular Expressions convert uppercase to lowercase

    Here's my code:

        
    
        Dim regEx As Object, strPattern As String, s_document As String    
        Dim new_string As String, sreturn As String
        Set regEx = CreateObject("vbscript.regexp")
    
    
        With regEx
            
            .Global = True
            .MultiLine = True
            .IgnoreCase = False
        
        End With
        
        s_document = ActiveDocument.Content.Text
        regEx.Pattern = "(-- )([A-Z])"
        ActiveDocument.Content.Text = regEx.Replace(s_document, "$1\l$2")
    The sample text I have is as follows:

    This is a -- A dog jumped over the moon -- actually this dog jumped over the moon -- No, this one.

    So from this code I'm trying to replace "-- A" with "-- a" and so on with the entire alphabet. My current result:

    a -- \lA

    So the flag \l isn't converting $2 to lowercase. Any guidance on this would be great. And as an extra, is there something I could throw in there to ignore if it's just "I" and to leave that as uppercase? Main goal is just to get the lowercase conversion working first.

    TYIA.

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Is there a reason for using RegEx, there is a LCase function in VBA.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Regular
    Joined
    Oct 2015
    Posts
    17
    Location
    Hey Bob. Thanks for taking the time to look at it and respond. Two reasons for RegEx. One, I want to learn how to implement it for future project ideas. Second, I plan on building more onto this function, where different values will be sent that may be a completely different pattern. As of right now my workaround solution is basically what you've said:



    For Each Match In Matches
            
            new_string = "-- " + LCase(Right(Match.Value, 1))
    
    
        Next

  4. #4
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Using RegEx that way will destroy your document's formatting. Any reason for not using Word's wildcard Find method? For example:
    Sub Demo()
    Application.ScreenUpdating = False
    With ActiveDocument.Range
      With .Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = "-- [A-Z]"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindStop
        .Format = False
        .MatchWildcards = True
      End With
      Do While .Find.Execute
        .Characters.Last.Text = LCase(Characters.Last.Text)
        .Collapse wdCollapseEnd
      Loop
    End With
    Application.ScreenUpdating = True
    End Sub
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  5. #5
    VBAX Regular
    Joined
    Oct 2015
    Posts
    17
    Location
    Macropod, I appreciate you pointing that out. I won't be using the content.text in the code. I realized shortly after how problematic that was. But again, my reasoning behind it is because I have other code that I'm going to be incorporating into this as well and the RegEx gives me a larger range of search parameters. As of right now I have it working through the use of the wildcard, but it's going to create a lot more lines of code if I have to handle each situation this way. If anyone has the solution to just change [A-Z] to a lower case letter that's all I need.

  6. #6
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Quote Originally Posted by xSmityx View Post
    RegEx gives me a larger range of search parameters.
    A wildcard Find/Replace can do a lot of what you can do with RegEx. It can also do things RegEx can't, such as incorporate formatting (font name, bold/italic, Style definition, etc.) as a Find and/or Replace parameter.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  7. #7
    VBAX Regular
    Joined
    Oct 2015
    Posts
    17
    Location
    Not disagreeing with you. I mostly use the find and replace. But for right now, I'm just trying to find out this one piece of the puzzle. The regEx builder I uses states \l\2 should change it to lowercase but it does work. I'm just asking around now to find out how to make that one part work. If you don't have that solution that's find, but that's what I'm looking for. I don't need an alternative simply because I've already got that.

Posting Permissions

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