Jump to content
PhoenixXL

StringRegExpReplaceEx BackRef UDF - Interface

Recommended Posts

PhoenixXL

BackRef UDF

Working

This UDF is made to provide a small interface towards the StringRegExpReplace Autoit function.

The extension is very limited but is an easy approach when used properly.

Functions

  • User can pass the complete back-reference to a function.
  • The return value from the function is then used to replace the specific group.
  • The final replacement is flexible and the user has full control over it.
  • The final replacement is done automatically till modification isn't required.
  • The optimization is easy and simple.
Limitations
  • The pattern has to consume the full string.
Example

Replace the vowels with their ASCII code

#include <BackRef.au3>


$i_BackRef_Debug = True ;Set Debug to true

; .. Example 1 ============================================================================================================================

; Replace the vowels with their ASCII code

Local $Replaced = RegExBackRef( 'Hello Nice Meeting You', '(.*?)([aeiou])(.*)', "AscW" ) ;The Function is inbuilt

If @error Then Exit @error

MsgBox ( 64, 'Test', $Replaced )

; ............ ============================================================================================================================

Make the first Number group equal to second number group if its greater than that

; .. Example 2 ============================================================================================================================

; The Function replaces the first group with third group if its greater than that

Local $Replaced = RegExBackRef( '1223 MaxRange : 150', '(\d+)([^:]+:\h+)(\d+)', "SetMaxRange", '\1:\3', "$v & '\2\3'" )

If @error Then Exit @error

MsgBox ( 64, 'Test', $Replaced )

; The User Defined Function
Func SetMaxRange( $iString )
$iString = StringSplit( $iString, ':' , 2)
$iString[0] = Int ( $iString [0] )
$iString[1] = Int ( $iString [1] )
If $iString[0] > $iString[1] Then Return $iString[1]
Return $iString[0]
EndFunc

; ............ ============================================================================================================================

The Function replaces unicode chars with ascii alphabet

; .. Example 3 ============================================================================================================================

; The Function replaces unicode version with ascii alphabet
Local $String = 'Déjà'

Local $Replaced

While 1
$Replaced = RegExBackRef($String, '(.*?)([àâäéèêë])(.*)', "ReplaceFunc")
If $Replaced = -1 Or @error Then ExitLoop
$String = $Replaced
WEnd

MsgBox(64, 'Test', $String)


Func ReplaceFunc($sMatch)
Switch $sMatch
Case "é","è","ê","ë"
Return "e"
Case "à","â","ä"
Return "a"
EndSwitch
EndFunc ;==>ReplaceFunc

; ............ ============================================================================================================================

Convert the unicode lower-case chars to upper-case

; .. Example 4 ============================================================================================================================
;Convert these chars to upper-case
Local $String = "à æ û ü"

MsgBox(64, 'Test', GlobalBackRef($String, '(.*?)([\340-\374])(.*)', "ReplaceUniChar"))

Func ReplaceUniChar($sMatch)
;Get the ASCII code of the string
$iAscW = AscW($sMatch)
;Return the character preceding the current character by 32
Return ChrW($iAscW - 32)
EndFunc ;==>ReplaceUniChar
; ............ ============================================================================================================================

ChangeLog

v1.0

-First Release

v1.1

-Added Debugging features

-Added Global Replacement

v1.2

-Added Count parameter

v1.3

-Now supports formatting control like \r \n and \t in the replace sequence.

Download - UDF(v1.3)

Here is the UDF to download in the following 7zip file.

Please post comments and do give me feedback for improvements and bugs.

v1.3 BackRef(UDF).7z

Previous Downloads : 44

Regards

Phoenix XL :)

Edited by PhoenixXL

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Share this post


Link to post
Share on other sites
PhoenixXL

Updated to v1.3

Regards :)

Edited by PhoenixXL

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Share this post


Link to post
Share on other sites
tcurran

Great! Exactly what I needed. Thank you!

Share this post


Link to post
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

  • Similar Content

    • MFrancisca
      By MFrancisca
      Hello! 
      I've been checking the logging UDFs in the wiki, mostly Log4a and Loga and I wanted to ask which one would you recommend for a script that will be executed remotely through PSExec. My main question is where the logs are located in that situation and if I can change that location to a custom one., because I need to retrieve them at the end of execution.
      So in a rough description the process is
      Open PSExec
      Send compiled AutoIT script to remote machine
      Execute script
      Copy logs from the remote to the local machine.
       
      Any opinions? 
    • gruntydatsun
      By gruntydatsun
      I have an XML file and every time there are three lines in a row with only <null/> in them, i want to insert a fourth line with <null/>.   Each line starts with 3 white spaces, followed by <null/> and ends with a white space followed by CR LF.   The presence of the three lines as described is unique to the points where I want to insert a line in this document.
       I'm trying to figure out how to apply the repeating part of a regex  {1,4} but apply it to this whole segment. 
      So far I have the below which picks up an individual line ok:
      ^\s{3}<null/>\s\r\n I tried wrapping it all in braces () then adding {3} but I'm obviously getting something wrong. 
      Attached is a section from the xml file with a block of nulls that should be matched if anyone would like to have a look.
      Help_From_Forum.xml
    • caramen
      By caramen
      I watched _OL_ItemSend&_OL_ItemFind&_OL_ItemCreate in OutlookEx UDF but hmmm
       
      Since it use the test environnement i cant get the orders of the mail creation
       
      Can someone make me win some time of reading all exemples script and lead me how to do one ?
      That whould be lovely.
      Gnight
    • Tersion
      By Tersion
      Here the this wiki page with list of available UDFs for data compression. For my tasks I only need ZIP support, so I started looking at pure AutoIt UDFs without any 3rd party dlls. And found out that most of available realizations uses standard ("native method") Windows dll - "zipfldr.dll". So for now I chose ZIP UDF by wraithdu. I've tested it on Windows 7 (x64) and it seem works fine. But here the comment from another topic where user says that Windows 10 discontinued support of "zipfldr.dll". Now I confused. I don't have around any Windows 10 machine to tested it. So maybe someone could confirm or deny that? Or maybe would better to switch to UDF with 7zip dll?
      I need an advice...
    • therks
      By therks
      So I have this pattern: 
      ^(?:(\d+)|(\d+):(\d+)|(\d+):(\d+):(\d+))$ And I'm expecting (depending on input) to get a 1, 2 or 3 index array (or @error for invalid input).
      But instead I get this:
      #include <Debug.au3> Func Test($String) _DebugArrayDisplay(StringRegExp($String, '^(?:(\d+)|(\d+):(\d+)|(\d+):(\d+):(\d+))$', 1)) EndFunc Test('10') ; Results (normal, expected): ; Row 0|10 Test('10:20') ; Results (extra blank index): ; Row 0| ; Row 1|10 ; Row 2|20 Test('10:20:30') ; Results (three blank indices): ; Row 0| ; Row 1| ; Row 2| ; Row 3|10 ; Row 4|20 ; Row 5|30 Is this normal? Should I just code around it, or is there a better way to do what I'm looking for?
      I also tried reversing my regex, but it was even uglier results:
      #include <Debug.au3> Func Test($String) _DebugArrayDisplay(StringRegExp($String, '^(?:(\d+):(\d+):(\d+))|(\d+):(\d+)|(\d+)$', 1)) EndFunc Test('10') ; Results (yuck): ; Row 0| ; Row 1| ; Row 2| ; Row 3| ; Row 4| ; Row 5|10 Test('10:20') ; Results (slightly better): ; Row 0| ; Row 1| ; Row 2| ; Row 3|10 ; Row 4|20 Test('10:20:30') ; Results (nice): ; Row 0|10 ; Row 1|20 ; Row 2|30  
×