Jump to content

PhoenixXL
 Share

Recommended Posts

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.

Link to comment
Share on other sites

  • 1 month later...

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.

Link to comment
Share on other sites

  • 2 years later...

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

×
×
  • Create New...