Name
PatternFindStr -- parse a string using pattern matching iterator function (V5.0)
Synopsis
func, state, val = PatternFindStr(s$, pat$[, encoding])
Function
This function can be used in conjunction with the generic For statement to parse the string specified in s$ according to the pattern specified in pat$. As required by the generic For statement, PatternFindStr() will return three values: An iterator function, a private state information, and an initial value for the traversal. Each time it is called, the iterator function will return the next captures from pattern pat$ over string s$. If pat$ specifies no captures, then the whole match is produced in each call.

The pattern specified in pat$ must adhere to the pattern syntax as described in the documentation of the PatternReplaceStr() function. See PatternReplaceStr for details.

See Generic For statement for details.

Starting with Hollywood 6.0, this function is also available in a version that can be used without a generic For statement. So if you only care about the first occurrence of pat$ in s$, then you might want to use PatternFindStrDirect() instead. See PatternFindStrDirect for details.

The optional encoding parameter can be used to set the character encoding to use. This defaults to the default string encoding set using SetDefaultEncoding(). See Character encodings for details.

Inputs
s$
string to parse
pat$
pattern according to which the string should be parsed
encoding
optional: character encoding to use (defaults to default string encoding) (V7.0)
Results
func
iterator function
state
private state information
val
initial traversal value
Example
s$ = "Hello World from Hollywood"
For w$ In PatternFindStr(s$, "%a+") Do DebugPrint(w$)
The above code will iterate over all the words from string s$, printing one per line.


t = {}
s$ = "Name=Andreas, Sex=Male, Nationality=German"
For k, v in PatternFindStr(s$, "(%w+)=(%w+)") Do t[k] = v
The above example collects all pairs key=value from the given string into a table.

Show TOC