PDA

View Full Version : [SOLVED:] Regular Expressions convert uppercase to lowercase



xSmityx
01-28-2022, 11:55 AM
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.

Bob Phillips
01-28-2022, 12:20 PM
Is there a reason for using RegEx, there is a LCase function in VBA.

xSmityx
01-28-2022, 02:41 PM
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

macropod
01-29-2022, 05:43 PM
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

xSmityx
01-29-2022, 08:09 PM
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.

macropod
01-29-2022, 08:24 PM
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.

xSmityx
01-29-2022, 09:14 PM
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.