Group string: Difference between revisions
imported>StDoodle mNo edit summary |
imported>Slyz No edit summary |
||
Line 22: | Line 22: | ||
needscode=yes| | needscode=yes| | ||
code1={{CodeSample| | |||
title=Code Sample| | |||
description= In this example, the regex tries to match a number followed by a space followed by a word. The number and the word are placed into groups.| | |||
code= | |||
<syntaxhighlight> | |||
string input = "134 Muscle 97 Mysticality 102 Moxie"; | |||
string [int,int] test = group_string( input, "(\\d+) (\\w+)" ); | |||
foreach i,j in test | |||
print("test[" + i + "][" + j + "] = " + test[i][j]); | |||
</syntaxhighlight> }} | |||
{{CodeSample| | |||
description= The first match was "134 Muscle", the first group was "134" and the second group was "Muscle". The resulting output is: | | |||
code= | |||
<syntaxhighlight> | |||
> call test.ash | |||
test[0][0] = 134 Muscle | |||
test[0][1] = 134 | |||
test[0][2] = Muscle | |||
test[1][0] = 97 Mysticality | |||
test[1][1] = 97 | |||
test[1][2] = Mysticality | |||
test[2][0] = 102 Moxie | |||
test[2][1] = 102 | |||
test[2][2] = Moxie | |||
</syntaxhighlight> }}| | |||
more_info= See [http://kolmafia.us/showthread.php?t=318 this thread] for details.| | more_info= See [http://kolmafia.us/showthread.php?t=318 this thread] for details.| |
Revision as of 14:30, 31 March 2010
Function Syntax
string [int, int] group_string(string group_me ,string group_by )
- group_me is the string to split into groups
- group_by is the regular expression to group by
This function finds all (non-overlapping) matches of the regular expression group_by within the string group_me. A 2-dimensional array of matching strings is returned. The first index is the number of the match, starting with 0 for the first match. The second index is the number of the group within each match, with exactly the same meaning as the 2nd parameter to group(): element 0 is the entire text matched by the regular expression, element 1 is the text matched by the first set of grouping parentheses within the expression, element 2 is the second set of grouping parentheses, and so on.
Code Sample
In this example, the regex tries to match a number followed by a space followed by a word. The number and the word are placed into groups.
string input = "134 Muscle 97 Mysticality 102 Moxie";
string [int,int] test = group_string( input, "(\\d+) (\\w+)" );
foreach i,j in test
print("test[" + i + "][" + j + "] = " + test[i][j]);
The first match was "134 Muscle", the first group was "134" and the second group was "Muscle". The resulting output is:
> call test.ash
test[0][0] = 134 Muscle
test[0][1] = 134
test[0][2] = Muscle
test[1][0] = 97 Mysticality
test[1][1] = 97
test[1][2] = Mysticality
test[2][0] = 102 Moxie
test[2][1] = 102
test[2][2] = Moxie
More Information
See this thread for details.
Attention KoLmafia Experts!
We need your help; some details of this function's operation are unknown or unclear.
The following specific questions have been raised:
- The regex isn't too hard to parse, but the results are.
- For results, I always see [int,0] and [int,1] keys; huh?