strfind: Difference between revisions

From RPTools Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 7: Line 7:
Functions related to strfind():
Functions related to strfind():


* [[getFindCount(id)]]
* [[getFindCount|getFindCount(id)]]
* [[getGroupCount(id)]]
* [[getGroupCount(id)]]
* [[getGroup(id, matchNo, groupNo)]]
* [[getGroup(id, matchNo, groupNo)]]

Revision as of 04:13, 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():

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"