Jump to content

StringReplace/StringRegExpReplace function problem


Go to solution Solved by jguinch,

Recommended Posts

Hi coders,
Hi Melba23 :sweating: ,

I have got a string (a href) "index.php?app=core&module=global"
all I need to do is assign php variables to autoit variables like
 

$app = "core"
$module = "global"

To get rid of string parts which I don't need, I tried to use function StringRegExpReplace but I got the following issue (as seen below the code):

StringRegExpReplace($sInput, "index.php?", "")

this should replace "index.php?" with nothing, but there is a logical operator "?" which remains untouched by the function.
 

I didn't find any help about ignoring logical operators so I tried some things and I found that [?] makes ? ignore.

StringRegExpReplace($sInput, "index.php[?]", "")

so this works (the function returns "" (nothing), that's what I need)

Is that the right way to do it, though?

 

Edited by knuxfighter
Link to comment
Share on other sites

Change your:

index.php?

To:

index.php?

The question mark in regular expression is used for a multitude of special functions. The backslash escapes the question mark in the regular expression and makes the function treat it as a normal character to use in the matching. Check the help for StringRegExp for more information.

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

  • Moderators

knuxfighter,

Welcome to the AutoIt forums - and there is no need to sweat over my presence unless you post something against the rules. ;)

I would do something like this:

$sInput = "index.php?app=core&module=global"

; Extract the "var=value" pairs
$aExtract = StringRegExp($sInput, "(\w*=\w*)", 3)

; Now loop through them
For $i = 0 To UBound($aExtract) - 1
    $sPHP = $aExtract[$i]
    ; Split on the "="
    $aSplit = StringSplit($sPHP, "=")
    ; Construct the new line
    $sAU3 = "$" & $aSplit[1] & ' = "' & $aSplit[2] & '"'
    ; And display it
    ConsoleWrite($sAu3 & @CRLF)
Next
You should see the required lines in the SciTE console. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

$sInput = "index.php?app=core&module=global"

$aArr = StringRegExp($sInput, "(\w*)=(\w*)", 3)

for $i = 0 to ubound($aArr) - 1 step 2
 assign($aArr[$i] , $aArr[$i + 1] , 2)
next

msgbox (0, '' , "$app = " & eval("app") & @CRLF & "$module = " & eval("module"))

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

Thank you all a lot for your answers, but this doesn't work if I do it with numbers (include minus numbers, too), I didn't know that one could be so complicated.
btw by the $app etc I mean putting the extracted values into variables for further use and assigning.

Link to comment
Share on other sites

  • Solution

$sInput = "index.php?var1=value1&var2=&var3=15&var4=-5"

$aExtract = StringRegExp($sInput, "[?&]([^=]+)=([^&]*)", 3)
For $i = 0 To UBound($aExtract) - 1 Step 2
    ; ConsoleWrite("$" & $aExtract[$i] & "=" & $aExtract[$i + 1] & @CRLF)
    Assign($aExtract[$i], $aExtract[$i + 1])
Next
 
ConsoleWrite("$var1=" & $var1 & @CRLF & _
             "$var2=" & $var2 & @CRLF & _
             "$var3=" & $var3 & @CRLF & _
             "$var4=" & $var4)

Link to comment
Share on other sites

And again, without the For-Next loop.

Local $sInput = "index.php?var1=value1&var2=&var3=15&var4=-5"

Local $sExtract = Execute("'" & StringRegExpReplace($sInput, ".*?[?&]([^=]+)=([^&]*)", "'&Assign('${1}','${2}')&'") & "'")

ConsoleWrite("$var1=" & $var1 & @CRLF & _
        "$var2=" & $var2 & @CRLF & _
        "$var3=" & $var3 & @CRLF & _
        "$var4=" & $var4 & @CRLF)
Link to comment
Share on other sites

....

And with quotes ?

Local $sInput = "index.php?var1='value1'&var2=&var3=15&var4=-5"
:P

 

The following example covers the two possibilities.  The variable value has either double quotes or single quotes.

;Local $sInput = 'index.php?var1="value1"&var2=&var3=15&var4=-5' ; Variable value with double quotes.
;Or
Local $sInput = "index.php?var1='value1'&var2=&var3=15&var4=-5" ; Variable value with single quotes.
;ConsoleWrite($sInput & @LF)

; If the variable value with no quotes, either Execute(string) can be used.
If StringInStr($sInput, '"') Then ; has double quotes
    ;ConsoleWrite("'" & StringRegExpReplace($sInput, ".*?[?&]([^=]+)=([^&]*)", "'&Assign('${1}','${2}')&'") & "'" & @LF)
    Local $sExtract = Execute("'" & StringRegExpReplace($sInput, ".*?[?&]([^=]+)=([^&]*)", "'&Assign('${1}','${2}')&'") & "'")
Else ; variable has single quotes
    ;ConsoleWrite('"' & StringRegExpReplace($sInput, ".*?[?&]([^=]+)=([^&]*)", '"&Assign("${1}","${2}")&"') & '"' & @LF)
    Local $sExtract = Execute('"' & StringRegExpReplace($sInput, ".*?[?&]([^=]+)=([^&]*)", '"&Assign("${1}","${2}")&"') & '"')
EndIf

ConsoleWrite('$var1=' & $var1 & @CRLF & _
        "$var2=" & $var2 & @CRLF & _
        "$var3=" & $var3 & @CRLF & _
        "$var4=" & $var4 & @CRLF)

Should the variable values have both double quotes and single quotes, then use jguinch's example.

Link to comment
Share on other sites

Local $sInput = "index.php?var1='value1'&var2=&var3=""15""&var4=-5" ; Variable value with both sigle and double quotes.

Local $sExtract = Execute('"' & StringRegExpReplace( StringReplace($sInput, "'", "''") , ".*?[?&]([^=]+)=([^&]*)", '"&Assign("${1}",''${2}'')&"') & '"')

ConsoleWrite('$var1=' & $var1 & @CRLF & _
        "$var2=" & $var2 & @CRLF & _
        "$var3=" & $var3 & @CRLF & _
        "$var4=" & $var4 & @CRLF)

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...