unsure of how too make this work

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
afbase
Forum Contributor
Posts: 113
Joined: Tue Aug 15, 2006 1:29 pm
Location: SoCAL!!!!

unsure of how too make this work

Post by afbase »

I am trying to create a Regex that will remove the "+" and "%" and select the number in the first example or simply just the "%" in the second example.
first example:

Code: Select all

<td>Company</td><td><span class="s4">+3.40%</span>
Second Example:

Code: Select all

<td>Company</td><td><span class="s5">-201.30%</span>


So far this is what I have for my regex:

Code: Select all

function mysql_past_5_earnings($ticker){
        $url = "http://moneycentral.msn.com/investor/invsub/analyst/earnest.asp?Page=EarningsGrowthRates&Symbol=".$ticker;
        $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL,$url);
                curl_setopt($ch, CURLOPT_FAILONERROR, 1);
                curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
                curl_setopt($ch, CURLOPT_TIMEOUT, 3);
        $result = curl_exec($ch);
                curl_close($ch);
        $pattern = '!<td>Company</td><td><span\s*class="s4">([^<]*)</span>!';
        preg_match($pattern,$result,$earnings);
        $this->past_5_earnings = preg_replace('#(+[^.]*).(^%*)|([^.]*).([^%]*)#', '$1$2', $earnings[1]);
}

The error returned from execution is:
Warning: preg_replace(): Compilation failed: nothing to repeat at offset 1 in /home/clinton/hdd1/coldowl/project/stock/tests/graham.php on line 190
Any help is appreciated :)
User avatar
GeertDD
Forum Contributor
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

Post by GeertDD »

Here's the regex I came up with, just by looking at the given example strings. The parenthese capture the number you want.

Code: Select all

(-?\d+\.\d+)%
Post Reply