Difference between revisions of "Create matcher"

From Kolmafia
Jump to navigation Jump to search
imported>Bale
imported>Ulti
(improving navigation)
 
(8 intermediate revisions by 3 users not shown)
Line 1: Line 1:
creating a new function
 
 
 
{{
 
{{
 
#vardefine:name|create_matcher}}{{
 
#vardefine:name|create_matcher}}{{
Line 7: Line 5:
 
FunctionPage|
 
FunctionPage|
 
name={{#var:name}}|
 
name={{#var:name}}|
function_category=String Handling Routines|
 
  
 
function1={{Function|
 
function1={{Function|
Line 24: Line 21:
 
needscode=yes|
 
needscode=yes|
  
 +
code1={{CodeSample|
 +
title=Code Sample|
 +
description=The following code will check if you are entitled to any new trophies and if so will attempt to buy them.|
 +
code=<syntaxhighlight>
 +
string trophypage = visit_url("trophy.php");
 +
matcher trophymatcher = create_matcher("name\=whichtrophy.*?value\=(.*?)\>",trophypage);
 +
while (find(trophymatcher)){
 +
  if (my_meat() >= 10000) {
 +
      string buytrophy=visit_url("trophy.php?action=buytrophy&whichtrophy="+group(trophymatcher,1),true);
 +
  } else {
 +
      print("Not enough meat","red");
 +
  }
 +
}
 +
</syntaxhighlight>}}|
 +
 +
see_also={{SeeAlso|find|replace_all|replace_first}}|
 
}}
 
}}
 +
 +
 +
 +
 +
[[Category:String Handling Routines]]

Latest revision as of 17:12, 14 October 2014

needs(code_samples);

Function Syntax

matcher create_matcher(string regex ,string input )

  • regex is the regular expression to use
  • input is the string to search with a regular expression

Creates a matcher from a pattern and input string.

Code Sample

The following code will check if you are entitled to any new trophies and if so will attempt to buy them.

string trophypage = visit_url("trophy.php");
matcher trophymatcher = create_matcher("name\=whichtrophy.*?value\=(.*?)\>",trophypage);
while (find(trophymatcher)){
   if (my_meat() >= 10000) {
      string buytrophy=visit_url("trophy.php?action=buytrophy&whichtrophy="+group(trophymatcher,1),true);
   } else {
      print("Not enough meat","red");
   }
}

See Also

find() | replace_all() | replace_first()