knuxfighter Posted March 29, 2015 Share Posted March 29, 2015 (edited) Hi coders, Hi Melba23 , 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 March 29, 2015 by knuxfighter Link to comment Share on other sites More sharing options...
SadBunny Posted March 29, 2015 Share Posted March 29, 2015 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 More sharing options...
knuxfighter Posted March 29, 2015 Author Share Posted March 29, 2015 Thanks Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted March 29, 2015 Moderators Share Posted March 29, 2015 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) NextYou should see the required lines in the SciTE console. M23 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
mikell Posted March 29, 2015 Share Posted March 29, 2015 (edited) #Include <Array.au3> $sInput = "index.php?app=core&module=global" $aExtract = StringRegExp($sInput, "(?:app|module)=(\w*)", 3) _ArrayDisplay($aExtract) Many ways to get the results, depending on their wanted further use Edited March 29, 2015 by mikell Link to comment Share on other sites More sharing options...
iamtheky Posted March 29, 2015 Share Posted March 29, 2015 $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 More sharing options...
knuxfighter Posted March 29, 2015 Author Share Posted March 29, 2015 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 More sharing options...
Solution jguinch Posted March 29, 2015 Solution Share Posted March 29, 2015 $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) Spoiler Network configuration UDF, _DirGetSizeByExtension, _UninstallList Firefox ConfigurationArray multi-dimensions, Printer Management UDF Link to comment Share on other sites More sharing options...
Malkey Posted March 29, 2015 Share Posted March 29, 2015 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 More sharing options...
jguinch Posted March 29, 2015 Share Posted March 29, 2015 @Malkey : nice ! And with quotes ? Local $sInput = "index.php?var1='value1'&var2=&var3=15&var4=-5" Spoiler Network configuration UDF, _DirGetSizeByExtension, _UninstallList Firefox ConfigurationArray multi-dimensions, Printer Management UDF Link to comment Share on other sites More sharing options...
Malkey Posted March 30, 2015 Share Posted March 30, 2015 .... And with quotes ? Local $sInput = "index.php?var1='value1'&var2=&var3=15&var4=-5" 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 More sharing options...
jguinch Posted March 30, 2015 Share Posted March 30, 2015 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) Spoiler Network configuration UDF, _DirGetSizeByExtension, _UninstallList Firefox ConfigurationArray multi-dimensions, Printer Management UDF Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now