Jump to content

StringSplit with multiple delimiters


Recommended Posts

I have a string in my ini file that looks like this:

text|123;text|456;text|789

and I am looking for a way only to extract the string "text" from the ini file.

I tried something like this:

$var = INIRead(@ScriptDir & "\temp.ini, "section", "key", "")
$split = StringSplit($var, ";"); i first split by semi-colon
$split2 = StringSplit($split[1], "|"); then i split by the "|" characters
For $i = 1 To $split2[0]
    MsgBox(0, "", $split2[$i])

msgbox() only returns the first text in the ini, it doesn't return all three..

where did i go wrong?

Edited by nowagain
Link to comment
Share on other sites

  • Moderators

#include <array.au3>
$var = "text|123;text|456;text|789"
$aSRE = StringRegExp($var, "(?s)(?m:^|;)(.+?)\|", 3)
_ArrayDisplay($aSRE)

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

  • Moderators

what if my ini starts out in a different format than what i told you.

it starts out usually in this format:

key=text;text;text

how would stringregexp() need to be changed?

Use weaponx's version or do some experiments on your own... Once you've exhausted yourself on trying to figure it out, post all that you've tried to receive assistance with RegExp's.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

what if my ini starts out in a different format than what i told you.

it starts out usually in this format:

key=text;text;text

how would stringregexp() need to be changed?

If you are using IniRead then "key=" will no longer be part of your string, it will only return "text;text;text"....

Link to comment
Share on other sites

Do you really mean only text, or are there other word than just text?

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to comment
Share on other sites

I have a string in my ini file that looks like this:

text|123;text|456;text|789

and I am looking for a way only to extract the string "text" from the ini file.

I tried something like this:

$var = INIRead(@ScriptDir & "\temp.ini, "section", "key", "")
$split = StringSplit($var, ";"); i first split by semi-colon
$split2 = StringSplit($split[1], "|"); then i split by the "|" characters
For $i = 1 To $split2[0]
    MsgBox(0, "", $split2[$i])

msgbox() only returns the first text in the ini, it doesn't return all three..

where did i go wrong?

A bit late but I notice that no one actually answered your question.

You went wrong here

For $i = 1 To $split2[0]

It should have been

For $i = 1 To $split[0]

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

It should have been

For $i = 1 To $split[0]

But then how would it have split by "|" this if it wasn't $split2[0]?

Edit: I think the easiest thing to do is to go with weaponx's way.

anyway...im getting some sleep, been up 24 hours...

Edited by nowagain
Link to comment
Share on other sites

#include <array.au3>

$var = "text|123;text|456;text|789"

$aSRE = StringRegExp($var, "\w+", 3)

_ArrayDisplay($aSRE)

;or

$aSRE = StringRegExp($var, "[^\|;0-9]+", 3)

_ArrayDisplay($aSRE)

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to comment
Share on other sites

process('text|123;text|456;text|789')

Func process($string)
    $delim1=StringSplit($string,';')
    $max=UBound($delim1)-1
    For $i=1 To $max
        $delim2=StringSplit($delim1[$i],'|')
        $text=$delim2[1]
        $number=$delim2[2]
        
        ;;Do Whatever Here
        MsgBox(0,'','Entry #'&$i&@CRLF&'Text: '&$text&@CRLF&'Number: '&$number)
        ;;Do Whatever Here
        
    Next
EndFunc

Edited by crashdemons

My Projects - WindowDarken (Darken except the active window) Yahsmosis Chat Client (Discontinued) StarShooter Game (Red alert! All hands to battlestations!) YMSG Protocol Support (Discontinued) Circular Keyboard and OSK example. (aka Iris KB) Target Screensaver Drive Toolbar Thingy Rollup Pro (Minimize-to-Titlebar & More!) 2D Launcher physics example Ascii Screenshot AutoIt3 Quine Example ("Is a Quine" is a Quine.) USB Lock (Another system keydrive - with a toast.)

Link to comment
Share on other sites

@crashdemons:

look at this:

process('text|123;text|456;text|789')

Func process($string)
    $delim1=StringSplit($string,';')
    $max=UBound($delim1)-1
    For $i=1 To $max
        $delim2=StringSplit($delim1[$i],'|')
        $text=$delim2[1]
        $number=$delim2[2]
       
       ;;Do Whatever Here
        MsgBox(0,'','Entry #'&$i&@CRLF&'Text: '&$text&@CRLF&'Number: '&$number)
       ;;Do Whatever Here
       
    Next
EndFunc

msgbox(0, "", $text)

its gives me an error that $text is "undeclared" when i put it in the msgbox()?

what if i only want that function to return just $text so i can used it "outside" the function.

what do i have to do?... :)

Link to comment
Share on other sites

@crashdemons:

look at this:

process('text|123;text|456;text|789')
 
 Func process($string)
     $delim1=StringSplit($string,';')
     $max=UBound($delim1)-1
     For $i=1 To $max
         $delim2=StringSplit($delim1[$i],'|')
         $text=$delim2[1]
         $number=$delim2[2]
        
       ;;Do Whatever Here
         MsgBox(0,'','Entry #'&$i&@CRLF&'Text: '&$text&@CRLF&'Number: '&$number)
       ;;Do Whatever Here
        
     Next
 EndFunc
 
 msgbox(0, "", $text)

its gives me an error that $text is "undeclared" when i put it in the msgbox()?

what if i only want that function to return just $text so i can used it "outside" the function.

what do i have to do?... :)

The scope of $text is limited to that function, move your call to msgbox...
Link to comment
Share on other sites

I dont understand what you mean by move my call.

You need to learn about variable scope first:

http://www.autoitscript.com/autoit3/docs/i...g_variables.htm

Variables defined within a function are considered local to said function (unless declared Global), meaning the variable does not exist outside the extents of Func...EndFunc.

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