MrKris1224 Posted September 25, 2015 Share Posted September 25, 2015 (edited) Hi, i need split string to array with space delimiter but without spliting strings in quotes and double quotes (' "). For example: The string is: Hello World "Hello World" i want to get array:[0] = 3[1] = Hello[2] = World[3] = Hello WorldAnd second example.The string is: 'Hello World' "Hello World (without " on end of string) and array:[0] = 3[1] = Hello World[2] = "Hello[3] = World Anybody have solution and can give me example code??PS. I'm polen and sorry for bad english. Edited September 25, 2015 by MrKris1224 Link to comment Share on other sites More sharing options...
mikell Posted September 25, 2015 Share Posted September 25, 2015 (edited) ?#Include <Array.au3> $s = 'Hello World "Helloo Woorld" This Is "an example" ok? ' $res = StringRegExp($s, '^|"([^"]*)"|\S+', 3) $res[0] = UBound($res)-1 _ArrayDisplay($res) Edited September 25, 2015 by mikell MrKris1224 1 Link to comment Share on other sites More sharing options...
MrKris1224 Posted September 25, 2015 Author Share Posted September 25, 2015 (edited) Thanks bro! I see u know StrinRegExp can u help me again? I need to replace some substring in string with some value. For example: the string is: %variable% %variable2% %other textI want replace %variable% with Eval(variable) and %variable2% with Eval(variable2), variable = Hello, variable2 = World and the output must be:Hello World %other text. just i want replace in string %anytext% with Eval("antytext") without mathing text which not have two %it's possible with StringRegExp? Edited September 25, 2015 by MrKris1224 Link to comment Share on other sites More sharing options...
ViciousXUSMC Posted September 25, 2015 Share Posted September 25, 2015 (edited) Heh figured mikell could come in with some amazing regex, I have a mess and its still not working.Edit: Almost perfect looks like "'hello world'" with single quotes will read into two arrays instead of staying together.#Include <File.au3> #Include <Array.au3> $sString ='Hello World "Hello World"' _ArrayDisplay(FindString($sString)) _ArrayDisplay(FindString2($sString)) $sString2 = "'" & "Hello World" & "'" & ' "' & "Hello World" _ArrayDisplay(FindString($sString2)) _ArrayDisplay(FindString2($sString2)) Func FindString($sString) $aResult1 = StringRegExp($sString,'(?i)["''][\w ]*["'']' , 3) $sResult1 = StringRegExpReplace($sString, '(?i)[''"][\w ]*[''"]', "") $aResult2 = StringRegExp($sResult1,"(?i)\b\w+\b" , 3) _ArrayConcatenate($aResult1, $aResult2) Return $aResult1 EndFunc Func FindString2($sString) $res = StringRegExp($sString, '^|"([^"]*)"|\S+', 3) $res[0] = UBound($res)-1 Return $res EndFunc Edited September 25, 2015 by ViciousXUSMC Link to comment Share on other sites More sharing options...
jguinch Posted September 25, 2015 Share Posted September 25, 2015 (edited) It's possible, but not directly with a regex replace :$var1 = "aaa" $var2 = "bbb" $string = "value for variable var1=%var1%, var2=%var2%" $newstring = Execute('"' & StringRegExpReplace(StringReplace($string, '"', '""'), "%([^%]+)%", '" & Eval(''$1'') & "' ) & '"') ConsoleWrite($newstring)edit : also possible (and simplier) with the ExpandVarStrings option :$var1 = "aaa" $var2 = "bbb" $iOldState = Opt("ExpandVarStrings", 1) $string = "value for variable var1=$var1$, var2=$var2$" ConsoleWrite($string) Opt("ExpandVarStrings", $iOldState) ; reaply the previous mode Edited September 25, 2015 by jguinch Spoiler Network configuration UDF, _DirGetSizeByExtension, _UninstallList Firefox ConfigurationArray multi-dimensions, Printer Management UDF Link to comment Share on other sites More sharing options...
MrKris1224 Posted September 25, 2015 Author Share Posted September 25, 2015 (edited) edit : also possible (and simplier) with the ExpandVarStrings option :$var1 = "aaa" $var2 = "bbb" $iOldState = Opt("ExpandVarStrings", 1) $string = "value for variable var1=$var1$, var2=$var2$" ConsoleWrite($string) Opt("ExpandVarStrings", $iOldState) ; reaply the previous mode Nice but i need % chars not $. I think first example is good but must don't match % if it's '^%' @ViciousXUSMCfor me example with StringRegExp working good. Edited September 25, 2015 by MrKris1224 Link to comment Share on other sites More sharing options...
jguinch Posted September 25, 2015 Share Posted September 25, 2015 Oups, sorry, Eval was not needed :$newstring = Execute('"' & StringRegExpReplace(StringReplace($string, '"', '""'), "%([^%]+)%", '" & $$1 & "' ) & '"')For '^%', do you refer to the ^ anchor or is it used as escape character ?; This ? $newstring = Execute('"' & StringRegExpReplace(StringReplace($string, '"', '""'), "(?<!\^)%(\w+)%", '" & $$1 & "' ) & '"') ; Or This ? $newstring = Execute('"' & StringRegExpReplace(StringReplace($string, '"', '""'), "(?<!^)%(\w+)%", '" & $$1 & "' ) & '"') MrKris1224 1 Spoiler Network configuration UDF, _DirGetSizeByExtension, _UninstallList Firefox ConfigurationArray multi-dimensions, Printer Management UDF Link to comment Share on other sites More sharing options...
MrKris1224 Posted September 25, 2015 Author Share Posted September 25, 2015 Thanks for help but i find solutiotin. Long but i have it: Opt("ExpandVarStrings", 1) $variable = " World" MsgBox(0,"", ParseVariables("Hello%variable%")) Func ParseVariables($szInput) Local $szOut For $i = 1 To StringLen($szInput) If StringMid($szInput, $i, 1) = "%" And StringMid($szInput, $i - 1, 1) <> "^" Then $szOut &= "$" Else $szOut &= StringMid($szInput, $i, 1) EndIf Next Return $szOut EndFunc Link to comment Share on other sites More sharing options...
TheSaint Posted September 25, 2015 Share Posted September 25, 2015 (edited) If you have trouble with RegEx, the simple but longer approach would be substitution and multiple splitting.EDITI was obviously slow clicking Reply. Edited September 25, 2015 by TheSaint Make sure brain is in gear before opening mouth! Remember, what is not said, can be just as important as what is said. Spoiler What is the Secret Key? Life is like a Donut If I put effort into communication, I expect you to read properly & fully, or just not comment. Ignoring those who try to divert conversation with irrelevancies. If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it. I'm only big and bad, to those who have an over-active imagination. I may have the Artistic Liesense to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage) Link to comment Share on other sites More sharing options...
MrKris1224 Posted September 25, 2015 Author Share Posted September 25, 2015 Topic to close. Link to comment Share on other sites More sharing options...
TLM Posted September 26, 2015 Share Posted September 26, 2015 how about this#Include <Array.au3> $s = 'Hello World "Helloo Woorld" This Is "an example" ok? ' $split = StringSplit($s, '"') Local $Rarray = $split For $C = 1 To $split[0] Step 1 $split2 = StringSplit($split[$C], " ") For $CC = 1 To $split2[0] Step 1 If $split2[$CC] <> "" Then _ArrayAdd($Rarray, $split2[$CC]) $Rarray[0] += 1 EndIf Next Next _ArrayDisplay($Rarray) 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