Jump to content
Sign in to follow this  
Uten

StringRegExpReplace, Several patterns

Recommended Posts

Uten

Now that topic tile was probably not self describing. This is what I want.

$r = StringRegExpReplace('This is a test "regarding" "back references" in a string', '"regarding"|"back references"', "$arg[\??]")
if $r = "This is a test $arg[1] $arg[2] in a string" Then 
    ; OK It works
Else
    ; hrmf, is it impossible?
EndIF

Replace \?? so it will return the back reference number (match number?) in the regexp pattern.

I think I have seen this in a sed script, and it might be possibly in perl. But does anyone know how to do it in pcre/Autoit?

Share this post


Link to post
Share on other sites
Xenobiologist

Hi,

do you mean this?

$r = StringRegExpReplace('This is a test "regarding" "back references" in a string', '"regarding"|"back references"', "$arg[$0$1]")
ConsoleWrite($r & @CRLF)

So long,

Mega


Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Share this post


Link to post
Share on other sites
Uten

No not exactly. I want to get the number of the back reference (hmm, at least I think thats what I want..:) ). The test should pass.

if $r = "This is a test $arg[1] $arg[2] in a string" Then

Share this post


Link to post
Share on other sites
Xenobiologist

No not exactly. I want to get the number of the back reference (hmm, at least I think thats what I want..:) ). The test should pass.

if $r = "This is a test $arg[1] $arg[2] in a string" Then
HI,

I do not get the sense in that. If you know you replace two strings then why not just write it the way it is?

So long,

Mega


Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Share this post


Link to post
Share on other sites
Uten

I want to parse and replace with variable references. I tried to provide the simplest possible test case. But I understand it might not have been to good or make sense to others.

Thanks for your input anyway. Actually I did not remember that $1..$9 (I'm using \1--\9)was the back reference in Autoit (and it is not documented as fare as I can see now) but I also think that $0$1 returns an unexpected result?

Why don't this work?

$r = StringRegExpReplace('This is a test "regarding" "back references" in a string', '"regarding"|"back references"', "$arg[$1]")
ConsoleWrite($r & @CRLF)

Share this post


Link to post
Share on other sites
Sokko

Actually I did not remember that $1..$9 (I'm using \1--\9)was the back reference in Autoit (and it is not documented as fare as I can see now)

It's 0 to 9, not 1 to 9. Both $ and \ are valid for back-references in StringRegExpReplace and they are both documented in the parameters section. In this case you have only one group in the pattern, so 0 is the only back-reference that makes sense.

(EDIT: Forum eats backslash-zero)

Edited by Sokko

Share this post


Link to post
Share on other sites
MrCreatoR

I think what you mean, is that will be multireplacment for all founded strings...

Try this function:

$String = 'This is a test "regarding" "back references" in a string'
Dim $Pattern[2] = ['"regarding"', '"back references"']
Dim $Replace[2] = ['$arg[1]', '$arg[2]']

$String = _StringMultiReplace($String, $Pattern, $Replace)

MsgBox (64, "Results", $String  & @LF & "Replacment number: " & @extended)

Func _StringMultiReplace($String, $Pattern, $Replace)
    If Not IsArray($Pattern) Or Not IsArray($Replace) Then Return SetError(1, 0, $String)
    Local $Extended = 0
    ReDim $Replace[UBound($Pattern)]
    For $i = 0 To UBound($Pattern)-1
        $String = StringReplace($String, $Pattern[$i], $Replace[$i])
        $Extended += @extended
    Next
    Return SetError(0, $Extended, $String)
EndFuncoÝ÷ Øò¢çhpj{]y©pØZ®¶²²Úò¶¬jëh×6$String = 'This is a test "regarding" "back references" in a string'
$Patern = '"regarding"|"back references"'
$Replace = '$arg[1]|$arg[2]'

$String = _StringMultiReplace($String, $Patern, $Replace)

MsgBox (64, "Results", $String & @LF & "Replacment number: " & @extended)

Func _StringMultiReplace($String, $Patern, $Replace)
    If StringInStr($Patern, '|') And StringInStr($Replace, '|') Then
        Local $PaternArr = StringSplit($Patern, '|')
        Local $ReplaceArr = StringSplit($Replace, '|')
        Local $Ret = $String, $Extended = 0
        Local $Ubound = UBound($ReplaceArr)
        If UBound($PaternArr) <= UBound($ReplaceArr) Then $Ubound = UBound($PaternArr)
        For $i = 1 To $Ubound-1
            $Ret = StringRegExpReplace($Ret, $PaternArr[$i], $ReplaceArr[$i])
            $Extended += @extended
        Next
        Return SetExtended($Extended, $Ret)
    Else
        Return StringRegExpReplace($String, $Patern, $Replace)
    EndIf
EndFunc

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

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
Sign in to follow this  

×