A Scripting Language for Web, Linux and Windows

A Scripting Language for Web, Linux and Windows

Example: Regular expressions

Demonstrates how to work with regex module based on PRCE and PHP compatible functions.

<?v1

require_once ("lib/wget.inc.v1");

function 
getStockValues () 
{
  
url "https://www.onvista.de";  

  
// Use WGet library to receive HTML website
  
website wget (urlnullfalse);
  if (
website["errText"]!="") {
    return 
"Error: ".website["errText"];
  }
  else {
    
// List of PCRE patterns
    
patternList = [
      
"Dow Jones" => '<a href="https:\/\/www.onvista.de\/index\/DB-DOW-JONES-INDIKATION-Index-324977#TopStocks"><span> Dow <\/span> <span> (.*?) <\/span>',
      
"German DAX" => '<a href="https:\/\/www.onvista.de\/index\/DAX-Index-20735#TopStocks"><span> DAX <\/span> <span> (.*?) <\/span>',
      
"Oil" => '<a href="https:\/\/www.onvista.de\/rohstoffe\/Oelpreis-WTI-26263303#TopStocks"><span> Öl <\/span> <span> (.*?) <\/span>',
      
"USDEUR" => '<a href="https:\/\/www.onvista.de\/devisen\/Dollarkurs-USD-EUR#TopStocks"><span> Dollar <\/span> <span> (.*?) <\/span>',
      
"Gold" => '<a title="Gold" href="https://www.onvista.de/rohstoffe/Goldpreis-26263275">Gold<\/a>  <\/td><td class="ZAHL"> <span data-push="31117890:last:1:0:Commodity">(.*?)<\/span>'      
    
];

    
stockList = array ();
    foreach (
patternList as name => pattern) {
      
matches = array ();      
      
// Check the pattern
      
ret  preg_match_all (patternwebsite["content"], matches);
      if (
ret) {
        
// Value found, remove . and replace , with .
        
stockList[name]=str_replace (array ("."","), array ("""."), matches[1][0]);
      }
      else {
        
stockList[name]="<not found, check pattern>";
      }
    }
    return 
stockList;
  }
  return -
1
}


print_r (getStockValues ());

?>

back to Home