Jump to content

StringRegExpReplace with Upper/Lower option


Recommended Posts

  • Moderators
Posted (edited)

Yep, one of those moments where I forgot that \u or \l doesn't work with our PCRE engine 😞.

So I *slap-sticked* some code together to achieve what I needed on the fly.

This is truly ugly IMO, but if you're not doing a lot of string manipulation concatenations it's ok.  I know I wrote some pcre funcs in a dll back in the day for my personal use to be able to do some things like this, but of course I can't find it... So native it is.

Anyway, I added this to my personal chars au3, thus the name you'll see.

Please test and criticize away, like I said, it was a quick mod and it's A-L-O-T of regex this and regex that ... but maybe it's useful for someone.

#include <StringConstants.au3>
#include <Array.au3>

; #FUNCTION# ====================================================================================================================
; Name ..........: _AutChars_RegExpReplaceEx
; Description ...: Allows StringRegExpReplace replace with pattern to do upper/lower
; Syntax ........: _AutChars_RegExpReplaceEx($sData, $sRegEx, $sReplace[, $iCount = 0])
; Parameters ....: $sData               - The string to check
;                  $sRegEx              - The regular expression to compare. See StringRegExp for pattern definition characters.
;                  $sReplace            - The text to replace the regular expression matching text with
;                  $iCount              - Number of replacements. Default is 0 for global replacement
; Return values .: Success              - an updated string based on regular expressions
;                                         @extended = number of replacements performed
; Author ........: SmOke_N (Ron Nielsen.. Ron.SMACKThatApp@GMail.com)
; Modified ......:
; Related .......: See StringRegExpReplace()
; Remarks .......: Will only work with \u or \l such as \u$2 in replace with pattern
;                  In my opinion, this is a pretty abusive way to achieve this, might be fine for small jobs
;                   but the overhead would be immense
; Example .......: _AutChars_RegExpReplaceEx("abcdAbcefg", "abc", "\u$0") will return ABCdAbcefg
; ===============================================================================================================================
Func _AutChars_RegExpReplaceEx($sData, $sRegEx, $sReplace, $iCount = 0)
    ; work in progress
    Local Const $iGlobalMatch = $STR_REGEXPARRAYGLOBALMATCH
    Local $sRet = ""

    Local Const $sMatchRE = "[\\$](?:u|l)[\$\\]{?\d+\}?"
    If Not StringRegExp($sReplace, $sMatchRE) Then
        $sRet = StringRegExpReplace($sData, $sRegEx, $sReplace, $iCount)
        Return SetError(@error, @extended, $sRet)
    EndIf

    ; unique identifier
    Local $sUStart = "<" & Chr(1) & "@U@" & Chr(1) & ">"
    Local $sUEnd =  "</" & Chr(1) & "@U@" & Chr(1) & ">"
    Local $sLStart = "<" & Chr(1) & "@L@" & Chr(1) & ">"
    Local $sLEnd =  "</" & Chr(1) & "@L@" & Chr(1) & ">"

    ; modify replace string
    Local $sTmp = $sReplace
    $sTmp = StringRegExpReplace($sTmp, "(?i)([\\$]u)([\$\\]{?\d+\}?)", $sUStart & "$2" & $sUEnd)
    $sTmp = StringRegExpReplace($sTmp, "(?i)([\\$]l)([\$\\]{?\d+\}?)", $sLStart & "$2" & $sLEnd)
    Local $sRepStr = StringRegExpReplace($sData, $sRegEx, $sTmp, $iCount)
    Local $iExtended = @extended

    ; get upper and lower matches with unique identifier
    Local $aMatchUpper = StringRegExp($sRepStr, "(\Q" & $sUStart & "\E.*?\Q" & $sUEnd & "\E)", $iGlobalMatch)
    Local $aMatchLower = StringRegExp($sRepStr, "(\Q" & $sLStart & "\E.*?\Q" & $sLEnd & "\E)", $iGlobalMatch)

    ; no need to worry about case
    Local $aMUpUnique, $aMLrUnique

    If IsArray($aMatchUpper) Then
        $aMUpUnique = _ArrayUnique($aMatchUpper, 0, 0, 0, $ARRAYUNIQUE_NOCOUNT)
    EndIf

    If IsArray($aMatchLower) Then
        $aMLrUnique = _ArrayUnique($aMatchLower, 0, 0, 0, $ARRAYUNIQUE_NOCOUNT)
    EndIf

    $sRet = $sRepStr
    Local $sMatch
    For $i = 0 To UBound($aMUpUnique) - 1
        $sMatch = StringRegExpReplace($aMUpUnique[$i], "\Q" & $sUStart & "\E|\Q" & $sUEnd & "\E", "")
        $sRet = StringReplace($sRet, $aMUpUnique[$i], StringUpper($sMatch), $iCount)
    Next
    For $i = 0 To UBound($aMLrUnique) - 1
        $sMatch = StringRegExpReplace($aMLrUnique[$i], "\Q" & $sLStart & "\E|\Q" & $sLEnd & "\E", "")
        $sRet = StringReplace($sRet, $aMLrUnique[$i], StringLower($sMatch), $iCount)
    Next

    Return SetExtended($iExtended, $sRet)
EndFunc

 

Edited by SmOke_N
Fixed issue with IsArray for upper values

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Thank you @SmOke_N, this is really helpful and I will definitely give it a try, making tests and enjoy it 😅 .

Best regards
Sven

Stay innovative!

Spoiler

🌍 Au3Forums

🎲 AutoIt (en) Cheat Sheet

📊 AutoIt limits/defaults

💎 Code Katas: [...] (comming soon)

🎭 Collection of GitHub users with AutoIt projects

🐞 False-Positives

🔮 Me on GitHub

💬 Opinion about new forum sub category

📑 UDF wiki list

✂ VSCode-AutoItSnippets

📑 WebDriver FAQs

👨‍🏫 WebDriver Tutorial (coming soon)

Link to comment
Share on other sites

$zz = _AutChars_RegExpReplaceEx("abcdAbcefg", "abc", "\u$0") ;will return ABCdAbcefg
ConsoleWrite("$zz=" & $zz & @CRLF)

Output:
$zz=<SOH@U@SOH>abc</SOH@U@SOH>dAbcefg


did I miss something?

I know that I know nothing

Link to comment
Share on other sites

  • Moderators

Yep, you found an issue,

If IsArray($aMUpUnique) Then

should have been

If IsArray($aMatchLower) Then

Thanks for finding it, not sure how I did that before I posted ... doh!

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...