Page 1 of 2
[SOLVED] Regex help
Posted: Sun Apr 17, 2005 8:25 am
by mjseaden
Hi there,
http://uk.finance.yahoo.com/q?d=v1&p=&q=q&s=DNX&m=L
I'm currently setting up my own 'money management' system so I know exactly what my financial situation is at any one time. The above URL points to a share information page on Yahoo!. The 'mid price' of a share is given on the Last Trade: XXX p cell.
Can anyone tell me a regex that will extract the midprice, XXX. I would be really grateful!
Many thanks
Mark
Posted: Sun Apr 17, 2005 8:36 am
by feyd
I think it's time for you to learn how to do the regex. I know I'm getting tired of writing regex for you...
Make some attempts at it.
Posted: Sun Apr 17, 2005 8:39 am
by mjseaden
okay feyd.
do you mind telling me how learned regex - where is a good resource i can look at? also, i'm on quite a tight timeline which is why i need assistance with it.
Mark
Posted: Sun Apr 17, 2005 8:55 am
by feyd
there are several threads linked to in the Useful Posts thread. There are resources linked to in the stickies as well.
Posted: Sun Apr 17, 2005 10:05 am
by mjseaden
Feyd,
I've read through some literature. I've done so in the past, but have found it very difficult to retain and also put it all together into the working whole.
Could you tell me what you think the regex should be for above and explain it? I work far better from example than from reading individual explanations of terms.
Many thanks
Mark
Posted: Sun Apr 17, 2005 11:25 am
by timvw
if i get it right you want to match
<tr><td class="yfnc_tablehead1" width="48%">Last Trade:</td><td class="yfnc_tabledata1"><big><b>562.00 p</b></big></td></tr>
now it's up to you to throw this in a RE. I'm pretty you will find some helpful tools that will show you instantly what you are matching etc... Makes finding the wanted RE painless...
it's sunday, so you have time for some homework :p
Posted: Sun Apr 17, 2005 2:40 pm
by mjseaden
So it would be something like
Code: Select all
#<tr><td class="yfnc_tablehead1" width="48\%">Last Trade:</td><td class="yfnc_tabledata1"><big><b>*[0-9].[0-9][0-9] p</b></big></td></tr>
Is that about right?
Many thanks
Mark
Posted: Sun Apr 17, 2005 3:26 pm
by timvw
mjseaden wrote:So it would be something like
Code: Select all
#<tr><td class="yfnc_tablehead1" width="48\%">Last Trade:</td><td class="yfnc_tabledata1"><big><b>*[0-9].[0-9][0-9] p</b></big></td></tr>
probably want *? to avoid greedy matching...
and then use (*?[0-9].[0-9][0-9]) so you get the actual number that is matched...
Posted: Sun Apr 17, 2005 6:36 pm
by m3rajk
mjseaden wrote:Feyd,
I've read through some literature. I've done so in the past, but have found it very difficult to retain and also put it all together into the working whole.
Could you tell me what you think the regex should be for above and explain it? I work far better from example than from reading individual explanations of terms.
Many thanks
Mark
people have written books on regex.
it's complicated, most hate it, it changes in every language.
however, php supports perl regex
the orielly learning perl has 3 chapters on regex.
it's also presented in a way that makes it rather easy to understand and pick up (or so i felt when i went and used that to learn perl and finally got regesx, something 4 different teachers in college seem to have no clue how to teach)
Posted: Fri Apr 22, 2005 10:29 am
by mjseaden
Hi,
I'm using the following regex
Code: Select all
$price = preg_match( '#<tr><td class="yfnc_tablehead1" width="48\%">Last Trade:</td><td class="yfnc_tabledata1"><big><b>(*?[0-9].[0-9][0-9]) p</b></big></td></tr>#', $content );
And getting the following response:
Code: Select all
Warning: Compilation failed: nothing to repeat at offset 98 in /home/spanishpp/public_html/personal/money.php on line 253
Is there an obvious cause to this error, which I don't understand?
Many thanks
Mark
Posted: Fri Apr 22, 2005 11:29 am
by mjseaden
Feyd, any chance for any pointers? Is the regex I have worked out correct?
Many thanks
Mark
Who's trying to get lots done!
Posted: Sat Apr 23, 2005 5:21 am
by mjseaden
Can anyone help??
Posted: Sat Apr 23, 2005 6:17 am
by timvw
Code: Select all
(*?ї0-9].ї0-9]ї0-9])
this prooves you are not even trying to find it yourself... or could you explain us what the following regular expression is supposed to do?
Posted: Sat Apr 23, 2005 8:26 am
by Chris Corbyn
OK I think we've given him enough of a bruising now... some people really just can't get their head around regex.
OK here it is BUT... please go away, break this down with some regex material to refer to and undersatnd how it works. These guys are right, it gets to a point where you'll have to learn from what's being shown to you.
<tr><td class="yfnc_tablehead1" width="48%">Last Trade:</td><td class="yfnc_tabledata1"><big><b>562.00 p</b></big></td></tr>
Code: Select all
#<tr><td class="e;yfnc_tablehead1"e; width="e;48\%"e;>Last Trade:</td><td class="e;yfnc_tabledata1"e;><big><b>(\d+\.\d{2}) p</b></big></td></tr>#s
Posted: Sat Apr 23, 2005 9:18 am
by mjseaden
Hi
The following code
Code: Select all
$yahoourl = 'http://uk.finance.yahoo.com/q?d=&s=DNX&m=L';
$fp = fopen( $yahoourl, 'r' );
$content = fread($fp, 30000);
preg_match( '#<tr><td class="yfnc_tablehead1" width="48\%">Last Trade:</td><td class="yfnc_tabledata1"><big><b>(\d+\.\d{2}) p</b></big></td></tr>#', $content, $matches );
fclose($fp);
echo '£ '.number_format($matches[0],2,'.',',');
Gives a result of zero - it isn't detecting the value properly. I can't see anything wrong with the PHP code, so I am assuming that the regex still doesn't work.
Mark