sonic208 Posted March 23, 2017 Share Posted March 23, 2017 how can i split a string like this ? P:4 |G: 1.2.0| L : 77770 |N: 0866746526 all i want is to store 1.2.0 in a variable $g, 77770 in $l and 08.. in $n https://www.autoitscript.com/autoit3/docs/functions/StringRegExp.htm i read the whole help file but i dont understand how to do it i appreciate every help or little hint Link to comment Share on other sites More sharing options...
Jfish Posted March 23, 2017 Share Posted March 23, 2017 There are better ways using regular expressions but this is a possible method to grab what you are after with loops and arrays, you can assign the variables from the last array at the end: include <Array.au3> $string="P:4 |G: 1.2.0| L : 77770 |N: 0866746526" $varArray=StringSplit($string,"|") _ArrayDisplay($varArray); before any cleansing for $a=0 to UBound($varArray)-1 $varArray[$a]=StringStripWS($varArray[$a],8) $varArray[$a]=StringTrimLeft($varArray[$a],2) Next _ArrayDisplay($varArray); this one shows you the data to assign sonic208 1 Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt Link to comment Share on other sites More sharing options...
kylomas Posted March 23, 2017 Share Posted March 23, 2017 What do you do with "P:4"? Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill Link to comment Share on other sites More sharing options...
sonic208 Posted March 23, 2017 Author Share Posted March 23, 2017 P:4 is not needed , it can be ignored but it can also be something like p:444 its not only one character, the other variables have always the same character count Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted March 23, 2017 Moderators Share Posted March 23, 2017 sonic208, This seems to work for the example string: #include <Array.au3> ; Just to display result #include <String.au3> $sString = "P:4 |G: 1.2.0| L : 77770 |N: 0866746526" $aRet = _StringBetween(StringStripWS($sString, $STR_STRIPALL) & "|", ":", "|") _ArrayDisplay($aRet, "", Default, 8) ; Just to display result 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...
Malkey Posted March 25, 2017 Share Posted March 25, 2017 Here are a couple of examples focusing on storing 1.2.0 in a variable $g, 77770 in $l, and, 08.. in $n . expandcollapse popup#include <Array.au3> Local $a, $b, $c, $d, $e, $f, $g, $h, $i, $j, $k, $l, $m, $n, $o, $p, $q, $r, $s, $t, $u, $v, $w, $x, $y, $z ; Copied from ConsoleWrite below. Local $string = "G: 1.2.0| L : 77770 |P:4 |N: 0866746526" ; Method 1 $varArray = StringRegExp($string, "(?:(?:\A|\|)\h*([A-Z])\h*:\h+([.0-9]+)\h*)", 3) ; Create 1D array of variables and their values. For $i = 0 To UBound($varArray) - 1 Step 2 $varArray[$i] = StringLower($varArray[$i]) Assign($varArray[$i], $varArray[$i + 1], 1) ; Assign value to lower case letter variable. ConsoleWrite('Eval("' & $varArray[$i] & '") = ' & Eval($varArray[$i]) & @CRLF) ; Display lower case letter variable and its value Next _ArrayDisplay($varArray) ; Or ;Method 2 Execute(StringTrimRight(StringRegExpReplace($string, "(?:\A|\|)\h*[A-Z]:[.0-9]+\h*|(?:(?:\A|\|)\h*([A-Z])\h*:\h+([.0-9]+)\h*)", 'Assign(StringLower("\1"), "\2", 1) & '), 3)) ; Assign value to lower case letter variable. ;ConsoleWrite(StringRegExpReplace($string, "(?:\A|\|)\h*[A-Z]:[.0-9]+\h*|(?:(?:\A|\|)\h*([A-Z])\h*:\h+([.0-9]+)\h*)", 'Assign(StringLower("\1"), "\2", 1) & ') & @CRLF) ; Shows the string that is trimmed, then executed. ;#cs ; ------ Display lower case letter variable and its value routine ------ $aVar = StringRegExp($string, "([A-Z ]+)(?=\:\h)", 3) For $i = 0 To UBound($aVar) - 1 $aVar[$i] = StringStripWS(StringLower($aVar[$i]), 8) ConsoleWrite('Eval("' & $aVar[$i] & '") = ' & Eval($aVar[$i]) & @CRLF) Next _ArrayDisplay($aVar) ;#ce ; ---- End of display lower case letter variable and its value routine ---- ; This above routine can be replaced with the last ConsoleWrite("$g = " & $g & ...) if the "Local $a to $z" is at the top of this script. ; Additional info Local $Loc = "Local " For $i = 1 To 26 $Loc &= "$" & Chr(96 + $i) & ", " Next ConsoleWrite(StringTrimRight($Loc, 2) & @CRLF) ; Copy to top of script. ;#cs ConsoleWrite("$g = " & $g & @CRLF & _ ; Without the above "Local $a to $z" not being copied to top of script, this "$l = " & $l & @CRLF & _ ; ConsoleWrite will not work. The variables must be declared before the "$n = " & $n & @CRLF) ; Assign command is used. However Eval will work without the initial declaration. ;#ce Link to comment Share on other sites More sharing options...
mikell Posted March 25, 2017 Share Posted March 25, 2017 My 2 cents - a little simpler #include <Array.au3> $sString = "P:4 |G: 1.2.0| L : 77770 |N: 0866746526" ; search a group of one or more dots and/or digits ; preceded by G,L or N and a sequence of one or more non-digit chars $res = StringRegExp($sString, '[GLN]\D+([\d.]+)', 3) _ArrayDisplay($res) Link to comment Share on other sites More sharing options...
iamtheky Posted March 25, 2017 Share Posted March 25, 2017 assigning values out of strings that are similar to the current example could be simple. $sString = "P:4 |G: 1.2.0| L : 77770 |N: 0866746526" $aStr = stringsplit(stringstripws($sString , 8) , "|" , 2) for $i = 0 to ubound($aStr) - 1 If stringleft($aStr[$i] , 1) = "G" OR stringleft($aStr[$i] , 1) = "L" OR stringleft($aStr[$i] , 1) = "N" Then assign(stringleft($aStr[$i] , 1) , stringsplit($aStr[$i] , ":" , 2)[1]) Next consolewrite(@CR & eval("G") & @CR & eval("L") & @CR & eval("N") & @CR) ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__) Link to comment Share on other sites More sharing options...
Malkey Posted March 26, 2017 Share Posted March 26, 2017 This example is based on a colon followed by a space after as the critical, decisive condition needed for selection of the variable and its value. Then the script assigns the value to the lowercase variable. #include <Array.au3> Local $a, $b, $c, $d, $e, $f, $g, $h, $i, $j, $k, $l, $m, $n, $o, $p, $q, $r, $s, $t, $u, $v, $w, $x, $y, $z Local $sString = "G: 1.2.0| L : 77770 |P:4 |N: 0866746526" ; search a group of one or more dots and/or digits ; preceded by a ":" and one or more spaces. $aValues = StringRegExp($sString, ':\h+([\d.]+)', 3) _ArrayDisplay($aValues) ; Find the upper case letter that is before an ; optional space, a colon, ":", and one or more spaces. $aVars = StringRegExp($sString, '([A-Z])\h*:\h+', 3) _ArrayDisplay($aVars) For $ii = 0 To UBound($aValues) - 1 Assign(StringLower($aVars[$ii]), $aValues[$ii]) Next MsgBox(0,"Results", "$g = " & $g & @CRLF & "$l = " & $l & @CRLF & "$n = " & $n) Note: Method #2 of post #6 does this in one line. Link to comment Share on other sites More sharing options...
mikell Posted March 26, 2017 Share Posted March 26, 2017 The same in one go for a precise answer to post #1 But as $g $n etc are already declared using Assign is not very useful #include <Array.au3> Local $g, $l, $n, $sString = "P:4 |G: 1.2.0| L : 77770 |N: 0866746526" ; search G, L or N followed by a sequence of one or more non-digit chars ; and then a group of one or more dots and/or digits $aValues = StringRegExp($sString, '([GLN])\D+([\d.]+)', 3) _ArrayDisplay($aValues) For $i = 0 To UBound($aValues) - 1 step 2 Assign(StringLower($aValues[$i]), $aValues[$i+1]) Next MsgBox(0,"Results", "$g = " & $g & @CRLF & "$l = " & $l & @CRLF & "$n = " & $n) 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