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

    • 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  
    • TheSaint
      By TheSaint
      Here is the bare bones of a UDF I have started work on.
      Mostly just a proof of concept at this stage, and still need to add some functions and dress the UDF up a bit ... to look like a UDF ... though it has my own distinct styling, especially as I have never really developed a UDF before now .... used plenty and modified plenty though. I've even invented my own UDF variable naming convention, which I am sure some of you will be aghast at. I work with what feels best for me, but others are free to adapt if they wish.
      The idea is to emulate the simplicity of INI files, but gain the benefits of SQL.
      Two scripts are provided.
      (1) The UDF, a work in progress - SimpleSQL_UDF.au3
      (2) An example or testing script - UDF_Test.au3
      Another first for me, is creating a 2D array from scratch, never done that before, that I can recall ... never had a need, and even for 1 dimension arrays, for a long time now, I have just used _StringSplit to create them. So I needed a bit of a refresher course, which my good buddy @TheDcoder assisted me with ... not without some angst I might add. LOL
      SimpleSQL_UDF.zip  (12 downloads previously)
      (I have now completed all the functions I intended to. My next update will be a big improvement, bringing things more inline with my latest INItoSQL DB program changes.)
      Program requires the sqlite3.dll, not included, but easily enough obtained.
      Hopefully the usage is self-evident ... just change the Job number variable in the UDF_Test.au3 file to check the existing functions out.
      Enjoy!
      P.S. This is also related to a new program I have just finished and uploaded - INItoSQL DB
    • milkmoron
      By milkmoron
      I am trying to search in a web browser dates XX/XX/XXXX that are also links. I want to click them after and remove them from the array. This is all I have so far. Nothing shows up. What am I doing wrong?
      ControlFocus ("Customer Center", "", "")
      Local $aArray = StringRegExp('(..)/(..)/(....)', '(..)/(..)/(....)', $STR_REGEXPARRAYFULLMATCH)
      For $i = 0 To UBound($aArray) - 1
          MsgBox($MB_SYSTEMMODAL, "RegExp Test with Option 2 - " & $i, $aArray[$i])
      Next
       
×