strfind: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
Line 8: | Line 8: | ||
* [[getFindCount|getFindCount(id)]] | * [[getFindCount|getFindCount(id)]] | ||
* [[getGroupCount(id)]] | * [[getGroupCount|getGroupCount(id)]] | ||
* [[getGroup(id, matchNo, groupNo)]] | * [[getGroup|getGroup(id, matchNo, groupNo)]] | ||
* [[getGroupStart(id, matchNo, groupNo)]] | * [[getGroupStart|getGroupStart(id, matchNo, groupNo)]] | ||
* [[getGroupEnd(id, matchNo, groupNo)]] | * [[getGroupEnd|getGroupEnd(id, matchNo, groupNo)]] | ||
Both matchNo and groupNo start at 1, the special group number 0 returns the whole pattern match. | Both matchNo and groupNo start at 1, the special group number 0 returns the whole pattern match. |
Revision as of 04:14, 24 March 2023
strfind() Function
• Introduced in version 1.3b48
Finds and extracts substrings from a string. strfind() is used to match a pattern against an input string and extract all of the capture groups. The function returns an id which can be used with other functions to extract the information. The supplied pattern is a regular expression.
Functions related to strfind():
- getFindCount(id)
- getGroupCount(id)
- getGroup(id, matchNo, groupNo)
- getGroupStart(id, matchNo, groupNo)
- getGroupEnd(id, matchNo, groupNo)
Both matchNo and groupNo start at 1, the special group number 0 returns the whole pattern match.
Groups are the regex capture groups, designated by the parts in "(" parenthesis ")". Group 1 returns the string that matches the first regex statement in (), 2 returns the second, etc.Usage
strfind(str, pattern)
Returns true
(1
) if the string starts with a certain substring and false
(0
) if not.
Examples
[h: id = strfind("This is a really useless test", "(\\S+)\\s+(\\S+)\\s*")]
[r: getGroupCount(id)]
[r: getFindCount(id)]
[r: getGroup(id, 1, 1)]
[r: getGroup(id, 2, 2)]
Returns
1 2 3 This really
A slightly more useful and advanced example:
[h:id = strfind("Command-20, Sleight of Hand 10, Knowledge (Arcana) +5", "([^,]*?)\\s?([-+]?\\d+)(,|\$)")]
<b>First group</b><br>
[r,count(getFindCount(id), "<b>Next group</b><br>"), code: {
"[r:getGroup(id, roll.count+1, 0)]" <br>
"[r:getGroup(id, roll.count+1, 1)]" <br>
"[r:getGroup(id, roll.count+1, 2)]" <br>
} ]
First group "Command-20," "Command" "-20" Next group " Sleight of Hand 10," " Sleight of Hand" "10" Next group " Knowledge (Arcana) +5" " Knowledge (Arcana)" "5"