MrKris1224

Split string with space delimiter

11 posts in this topic

#1 ·  Posted (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 World

And 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 by MrKris1224

Share this post


Link to post
Share on other sites



#2 ·  Posted (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 by mikell
1 person likes this

Share this post


Link to post
Share on other sites

#3 ·  Posted (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 text

I 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 by MrKris1224

Share this post


Link to post
Share on other sites

#4 ·  Posted (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 by ViciousXUSMC

Share this post


Link to post
Share on other sites

#5 ·  Posted (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 by jguinch

Share this post


Link to post
Share on other sites

#6 ·  Posted (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 '^%'

 

@ViciousXUSMC

for me example with StringRegExp working good.

Edited by MrKris1224

Share this post


Link to post
Share on other sites

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 & "' ) & '"')

 

 

1 person likes this

Share this post


Link to post
Share on other sites

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

 

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

If you have trouble with RegEx, the simple but longer approach would be substitution and multiple splitting.

EDIT

I was obviously slow clicking Reply.

Edited by TheSaint

AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

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

userbar.png

Share this post


Link to post
Share on other sites

Topic to close.

Share this post


Link to post
Share on other sites

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)

 

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