func, state, val = PatternFindStr(s$, pat$[, encoding])
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.
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] = vThe above example collects all pairs key=value from the given string into a table.