strfind: Difference between revisions

From RPTools Wiki
Jump to navigation Jump to search
(Created page with "== == • Introduced in version 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, matc...")
 
No edit summary
Line 1: Line 1:
== ==
{{MacroFunction
|name=strfind
|version=1.3b48
|description=
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.


• Introduced in version
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():
Functions related to strfind():


Line 13: Line 15:


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.
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
 
|usage=
 
<syntaxhighlight lang="mtmacro" line>
strfind(str,  pattern)
strfind(str,  pattern)
Example
</syntaxhighlight>
 
Returns {{true}} if the string starts with a certain substring and {{false}} if not.
 
|examples=
<syntaxhighlight lang="mtmacro" line>
     [h: id = strfind("This is a really useless test", "(\\S+)\\s+(\\S+)\\s*")]  
     [h: id = strfind("This is a really useless test", "(\\S+)\\s+(\\S+)\\s*")]  
     [r: getGroupCount(id)]
     [r: getGroupCount(id)]
Line 21: Line 31:
     [r: getGroup(id, 1, 1)]   
     [r: getGroup(id, 1, 1)]   
     [r: getGroup(id, 2, 2)]
     [r: getGroup(id, 2, 2)]
Returns:
</syntaxhighlight>


'''Returns'''
<pre>  1
   2  
   2  
   3  
   3  
   This  
   This  
   really  
   really  
</pre>
A slightly more useful and advanced example:


Returns:
<syntaxhighlight>
[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>
} ]
</syntaxhighlight>


<pre>
   First group  
   First group  
   "Command-20,"  
   "Command-20,"  
Line 41: Line 65:
   " Knowledge (Arcana) +5"  
   " Knowledge (Arcana) +5"  
   " Knowledge (Arcana)"  
   " Knowledge (Arcana)"  
   "5"  
   "5"
.
</pre>
}}

Revision as of 04:11, 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"