Juggler_IN
Active Member
- Joined
- Nov 19, 2014
- Messages
- 358
- Office Version
- 2003 or older
- Platform
- Windows
I want help with the attached function ... It takes a textual string specified by the argument x and converts every field masked with \ to its Unicode code unless it is already Unicode or masked with \. The code replaces the first match of the regular expression from the end of the string to the beginning of the string. The rest of the string remains unchanged. For example,
— RegExChr("\a\b\c\1\2\3") outputs “\a\b\c\1\2\u0033\.”
— RegExChr("\a\b\c\1\2\u0033\") outputs “\a\b\c\1\u0032\u0033\.”
— RegExChr("\a\b\c\1\u0032\u0033\") outputs “\a\b\c\u0031\u0032\u0033\.”
But, if I add the chr "\" ... is not getting converted to its Unicode value. That is,
?RegExChr("\a\b\c\1\2\3\\") gives \a\b\c\1\2\3\ but what I want is \a\b\c\1\2\3\u005C\.
?RegExChr("\a\b\c\1\2\3\u005C\") gives the expected \a\b\c\1\2\u0033\u005C\.
I tried removing Replace(RegExChr, "\\", "\") but still do not the output desired.
Any thoughts?
— RegExChr("\a\b\c\1\2\3") outputs “\a\b\c\1\2\u0033\.”
— RegExChr("\a\b\c\1\2\u0033\") outputs “\a\b\c\1\u0032\u0033\.”
— RegExChr("\a\b\c\1\u0032\u0033\") outputs “\a\b\c\u0031\u0032\u0033\.”
But, if I add the chr "\" ... is not getting converted to its Unicode value. That is,
?RegExChr("\a\b\c\1\2\3\\") gives \a\b\c\1\2\3\ but what I want is \a\b\c\1\2\3\u005C\.
?RegExChr("\a\b\c\1\2\3\u005C\") gives the expected \a\b\c\1\2\u0033\u005C\.
I tried removing Replace(RegExChr, "\\", "\") but still do not the output desired.
Any thoughts?
VBA Code:
Function RegExChr(ByVal x As String) As String
Dim oRegex As Object, i As Double, oCarry As Object
If oRegex Is Nothing Then
Set oRegex = CreateObject("vbScript.RegExp")
oRegex.Global = True
oRegex.Pattern = "(?:\\(\\)|\\u[0-9A-F]{4}|\\(?!\\)(.))"
End If
RegExChr = x
Set oCarry = oRegex.Execute(x)
For i = oCarry.Count - 1 To 0 Step -1
If oCarry(i).SubMatches(1) <> Empty Then
RegExChr = RegExHlp(oCarry(i), RegExChr, RegExAsc(oCarry(i).SubMatches(1)) & "\")
RegExChr = Replace(RegExChr, "\\", "\")
Exit Function
ElseIf oCarry(i).SubMatches(0) = "\" Then
RegExChr = RegExHlp(oCarry(i), RegExChr, "\")
RegExChr = Replace(RegExChr, "\\", "\")
Exit Function
End If
Next i
End Function
Function RegExHlp(ByRef Match As Object, ByVal s As String, ByVal r As String) As String
RegExHlp = Left(s, Match.FirstIndex) & r & Mid(s, Match.FirstIndex + Match.Length + 1)
End Function
Function RegExAsc(ByVal s As String) As String
If AscW(s) > 256& * 256& - 1 Then
Else
RegExAsc = "\u" & String(4 - Len(Hex(AscW(s))), "0") & Hex(AscW(s))
End If
End Function