Loop Parse

Retrieves substrings (fields) from a string, one at a time.

Loop Parse String , Delimiters, OmitChars

Parameters

String

Type: String

The string to analyze.

Delimiters

Type: String

If this parameter is blank or omitted, each character of the input string will be treated as a separate substring.

If this parameter is "CSV", the string will be parsed in standard comma separated value format. Here is an example of a CSV line produced by MS Excel:

"first field",SecondField,"the word ""special"" is quoted literally",,"last field, has literal comma"

Otherwise, Delimiters contains one or more characters (case sensitive), each of which is used to determine where the boundaries between substrings occur.

Delimiter characters are not considered to be part of the substrings themselves. In addition, if there is nothing between a pair of delimiters within the input string, the corresponding substring will be empty.

For example: ',' (a comma) would divide the string based on every occurrence of a comma. Similarly, A_Tab A_Space would start a new substring every time a space or tab is encountered in the input string.

To use a string as a delimiter rather than a character, first use StrReplace to replace all occurrences of the string with a single character that is never used literally in the text, e.g. one of these special characters: ¢¤¥¦§©ª«®µ¶. Consider this example, which uses the string <br> as a delimiter:

NewHTML := StrReplace(HTMLString, "<br>", "¢")
Loop Parse, NewHTML, "¢" ; Parse the string based on the cent symbol.
{
...
}
OmitChars

Type: String

An optional list of characters (case sensitive) to exclude from the beginning and end of each substring. For example, if OmitChars is A_Space A_Tab, spaces and tabs will be removed from the beginning and end (but not the middle) of every retrieved substring.

If Delimiters is blank, OmitChars indicates which characters should be excluded from consideration (the loop will not see them).

Remarks

A string parsing loop is useful when you want to operate on each field contained in a string, one at a time. Parsing loops use less memory than StrSplit (though either way the memory use is temporary) and in most cases they are easier to use.

The built-in variable A_LoopField exists within any parsing loop. It contains the contents of the current substring (field). If an inner parsing loop is enclosed by an outer parsing loop, the innermost loop's field will take precedence.

Although there is no built-in variable "A_LoopDelimiter", the example at the very bottom of this page demonstrates how to detect which delimiter was encountered for each field.

There is no restriction on the size of the input string or its fields.

To arrange the fields in a different order prior to parsing, use the Sort function.

See Loop for information about Blocks, Break, Continue, and the A_Index variable (which exists in every type of loop).

Related

StrSplit, file-reading loop, Loop, Break, Continue, Blocks, Sort, FileSetAttrib, FileSetTime

Examples

#1

Colors := "red,green,blue"
Loop parse, Colors, ","
{
    MsgBox "Color number " A_Index " is " A_LoopField
}

#2: Read the lines inside a variable, one by one (similar to a file-reading loop). A file can be loaded into a variable via FileRead:

Loop parse, FileContents, "`n", "`r"  ; Specifying `n prior to `r allows both Windows and Unix files to be parsed.
{
    Result := MsgBox("Line number " A_Index " is " A_LoopField ".`n`nContinue?",, "y/n")
}
until Result = "No"

#3: This is the same as the example above except that it's for the clipboard. It's useful whenever the clipboard contains files, such as those copied from an open Explorer window (the program automatically converts such files to their file names):

Loop parse, A_Clipboard, "`n", "`r"
{
    Result := MsgBox("File number " A_Index " is " A_LoopField ".`n`nContinue?",, "y/n")
}
until Result = "No"

#4: Parse a comma separated value (CSV) file:

Loop read, "C:\Database Export.csv"
{
    LineNumber := A_Index
    Loop parse, A_LoopReadLine, "CSV"
    {
        Result := MsgBox("Field " LineNumber "-" A_Index " is:`n" A_LoopField "`n`nContinue?",, "y/n")
        if Result = "No"
            return
    }
}

#5: Determining which delimiter was encountered.

; Initialize string to search.
Colors := "red,green|blue;yellow|cyan,magenta"
; Initialize counter to keep track of our position in the string.
Position := 0

Loop Parse, Colors, ",|;"
{
    ; Calculate the position of the delimiter at the end of this field.
    Position += StrLen(A_LoopField) + 1
    ; Retrieve the delimiter found by the parsing loop.
    Delimiter := SubStr(Colors, Position, 1)

    MsgBox "Field: " A_LoopField "`nDelimiter: " Delimiter
}