简体   繁体   中英

Matching regular expression

I am trying to get the data from all the div's with price in them in the following section of code:

<section data-v-7630ac7c="" view-more-component="">
<div data-v-5c5571e4="" table-component="" data-v-7630ac7c="">
<header data-v-5c5571e4="">
<div data-v-5c5571e4="" price="" right-align="">Price</div> 
<div data-v-5c5571e4="" quantity="" right-align="">Quantity</div> 
<div data-v-5c5571e4="" date="">Time</div></header> <section data-v-5c5571e4="">
<div data-v-5c5571e4="" price="" right-align="" style="color: rgb(142, 201, 25);">8611.00</div> 
<div data-v-5c5571e4="" quantity="" right-align="">1.00000000</div> 
<div data-v-5c5571e4="" date="">13:27:17</div></section><section data-v-5c5571e4="">
<div data-v-5c5571e4="" price="" right-align="" style="color: rgb(142, 201, 25);">8612.30</div> 
<div data-v-5c5571e4="" quantity="" right-align="">1.00000000</div> 
<div data-v-5c5571e4="" date="">13:03:12</div></section><section data-v-5c5571e4="">
<div data-v-5c5571e4="" price="" right-align="" style="color: rgb(255, 42, 104);">8517.70</div> 
<div data-v-5c5571e4="" quantity="" right-align="">0.04983464</div> 
<div data-v-5c5571e4="" date="">12:51:24</div></section><section data-v-5c5571e4="">
<div data-v-5c5571e4="" price="" right-align="" style="color: rgb(142, 201, 25);">8608.70</div> 
<div data-v-5c5571e4="" quantity="" right-align="">0.59195657</div> 
<div data-v-5c5571e4="" date="">12:32:26</div></section><section data-v-5c5571e4="">
<div data-v-5c5571e4="" price="" right-align="" style="color: rgb(255, 42, 104);">8521.70</div> 
<div data-v-5c5571e4="" quantity="" right-align="">0.02990000</div> 
<div data-v-5c5571e4="" date="">11:29:57</div></section><section data-v-5c5571e4="">
<div data-v-5c5571e4="" price="" right-align="" style="color: rgb(142, 201, 25);">8545.20</div> 
<div data-v-5c5571e4="" quantity="" right-align="">0.26101049</div> 
<div data-v-5c5571e4="" date="">01:20:35</div></section><section data-v-5c5571e4="">
<div data-v-5c5571e4="" price="" right-align="" style="color: rgb(255, 42, 104);">8462.30</div> 
<div data-v-5c5571e4="" quantity="" right-align="">0.00674370</div> 
<div data-v-5c5571e4="" date="">23:49:47</div></section><section data-v-5c5571e4="">
<div data-v-5c5571e4="" price="" right-align="" style="color: rgb(142, 201, 25);">8539.30</div> 
<div data-v-5c5571e4="" quantity="" right-align="">0.05799985</div> 
<div data-v-5c5571e4="" date="">23:00:53</div></section><section data-v-5c5571e4="">
<div data-v-5c5571e4="" price="" right-align="" style="color: rgb(255, 42, 104);">8446.00</div> 
<div data-v-5c5571e4="" quantity="" right-align="">0.02345439</div> 
<div data-v-5c5571e4="" date="">22:30:43</div></section><section data-v-5c5571e4="">
<div data-v-5c5571e4="" price="" right-align="" style="color: rgb(255, 42, 104);">8540.00</div> 
<div data-v-5c5571e4="" quantity="" right-align="">0.25500001</div> 
<div data-v-5c5571e4="" date="">21:55:28</div></section></div> 
<section data-v-7630ac7c="" id="my-trades"><div data-v-75ce2ffa="" data-v-7630ac7c="" button-component="" class="viewmore fullbutton">
<button data-v-75ce2ffa=""></button> 
<!----> <span data-v-75ce2ffa="" text="">View More</span> <!----></div> <!----></section> <!----> <!----></section> <!---->

The regular expression i am using is

<div (..................) price="" (.*)">(.*)<\/div>

But I keep getting NULL. Any help will be really appreciated.

Although parsing html with regex is strongly discouraged your posted html code only contains div tags without nesting so you can apply this simple regex and do your job of getting all prices from div tags:

<div.*?price.*?>(\d+\.\d+)<\/div>

But keep this as a temporary fix and if you have to use it in long run then go for an html parser.

Demo,

https://regex101.com/r/JBwRM5/1

I tried both they didnt work here is the whole code i am using

<?
$curl = curl_init();
$SearchString = "BTCCAD";
$url = "https://einstein.exchange/trade?mode=advanced&instrument=$SearchString";
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
$result = curl_exec($curl);
preg_match_all('<div[^>]+price=\"\"[^>]+>([\d.]+)<',$result,$matches);
var_dump($matches);
curl_close($curl);
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM