Loop Parse

文字列から部分文字列(フィールド)を1つずつ取得します。

Loop Parse String , DelimiterChars, OmitChars

パラメータ

String

型:文字列

解析する文字列です。

DelimiterChars

型:文字列

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

このパラメータが"CSV"の場合、文字列は標準的なカンマ区切り値形式で解析されます。ここでは、MS Excelで作成したCSVの行の例を示します:

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

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

デリミタ文字は、部分文字列自体の一部とはみなされません。In addition, if there is nothing between a pair of delimiter characters within the input string, the corresponding substring will be empty.

事例:','(カンマ)は、カンマが出現するたびに文字列を分割することになります。Similarly, A_Space A_Tab would start a new substring every time a space or tab is encountered in the input string.

文字列を文字ではなく区切り文字として使用するには、まずStrReplaceを使用して、文字列のすべての出現箇所を、テキスト内で文字通り使用されることのない単一の文字(例えば、これらの特殊文字のいずれか)に置き換えます:¢¤¥¦§©ª«®µ¶. 文字列<br> をデリミタとして使用する例を考えてみましょう:

NewHTML := StrReplace(HTMLString, "<br>", "¢")
Loop Parse, NewHTML, "¢" ; セント記号を元に文字列を解析します。
{
    ; ...
}
OmitChars

型:文字列

If blank or omitted, no characters will be excluded. Otherwise, specify a list of characters (case-sensitive) to exclude from the beginning and end of each substring. 例えば、OmitCharsA_Space A_Tabの場合、検索されたすべての部分文字列の先頭と末尾からスペースとタブが削除されます(中央は削除されない)。

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

備考

文字列解析ループは、文字列に含まれる各フィールドを一度に操作したい場合に便利です。解析ループはStrSplitよりもメモリ使用量が少なく(どちらにしてもメモリ使用は一時的ですが)、ほとんどの場合、使い勝手が良いです。

組込変数A_LoopFieldは、任意の構文解析ループ内に存在します。現在の部分文字列(フィールド)の内容を含んでいます。内側の解析ループが外側の解析ループに囲まれている場合、最も内側のループのフィールドが優先されます。

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

入力文字列やそのフィールドのサイズに制限はありません。

解析前にフィールドを別の順序で並べるには、ソート機能を使用します。

BlocksBreakContinue、A_Index変数(どのタイプのループにも存在する)については、「Loop」をご覧ください。

ループの後にElse文が続くこともあり、ループの反復回数がゼロの場合に実行されます。Note that the loop always has at least one iteration unless String is empty or DelimiterChars is omitted and all characters in String are included in OmitChars.

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

カンマ区切りの文字列をパースします。

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

Reads the lines inside a variable, one by one (similar to a file-reading loop). FileReadにより、ファイルを変数に読み込むことができます。

Loop parse, FileContents, "`n", "`r"  ; `r の前に `n を指定すると、Windows と Unix の両方のファイルをパースすることができます。
{
    Result := MsgBox("Line number " A_Index " is " A_LoopField ".`n`nContinue?",, "y/n")
}
until Result = "No"

This is the same as the example above except that it's for the clipboard. クリップボードにファイルが含まれている場合、例えば開いているエクスプローラーウィンドウからコピーされたファイル(プログラムは自動的にファイル名に変換します)などは、この機能を使うと便利です。

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

CSV(Comma Separated Value)ファイルを解析します。

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

Determines which delimiter character was encountered.

; 検索する文字列を初期化します。
Colors := "red,green|blue;yellow|cyan,magenta"
; 文字列中の位置を記録するためのカウンターを初期化します。
Position := 0

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

MsgBox "Field:" A_LoopField "`nDelimiter character:" DelimiterChar
}