1

I should have mentioned that I use preg_match. It is a fix for a larger code, and switching method now will be a big headache.

I am trying to create a regex matching :

<span class="paper-name">some string</span>\r    </h1>\r    <div class="data-row">\r        <span class="num">0.25

What I really need is that last part - the number. it can be from 0.00 to 9.99.

I tried to break it to:

<span class="paper-name">some string</span>
<div class="data-row">
<span class="num">
0.25

And find all in between No luck. Can someone please help?

I so so close: I can find the first part:

^.*(?<=(<span class="paper-name">some string<))

And the second part :

<))(?.*)(span\sclass\=\"num"\>(\d*\.\d*))

But i am not able to connect them. I need the first appearance after the first part of the second part.

Hellpless
  • 88
  • 2
  • 12
  • Try this reg exp ```/[0-9]+\.[0-9]{2}$/g``` – Shashidhara Nov 27 '18 at 11:20
  • Possible duplicate of [Extract numbers from a string](https://stackoverflow.com/questions/6278296/extract-numbers-from-a-string) – RainDev Nov 27 '18 at 11:21
  • 2
    HTML and regex are not good friends. Use a parser, it is simpler, faster and much more maintainable. See: http://php.net/manual/en/class.domdocument.php – Toto Nov 27 '18 at 11:26

3 Answers3

4

Use a parser way instead:

<?php

# html string
$html = <<<DATA
<h1>
    <span class="paper-name">some string</span>
</h1>
<div class="data-row">
    <span class="num">0.25</span>
</div>
DATA;

# set up the domdocument
$dom = new DomDocument();
$dom->loadHTML($html);

# the xpath object
$xpath = new DOMXpath($dom);

# query the dom
$numbers = $xpath->query("//span[@class = 'num']");

# iterate over the results
foreach ($numbers as $number) {
    echo $number->nodeValue;
}

This correctly yields

0.25

and will work for most other HTML snippets as well whereas a regular expression might not.


Edit:

If you insist on using regular expressions (why?), you might try:

<span class="num">\K\d(?:\.\d{2})?

See a demo on regex101.com.

Jan
  • 42,290
  • 8
  • 54
  • 79
0

If you using regular expressions then try https://regex101.com/r/DGbVbp/1/:

$html = '<h1>
    <span class="paper-name">some string</span>
</h1>
<div class="data-row">
    <span class="num">


0.25

</span>
<div class="data-row">
    <span class="num">


0.22

</span>
</div>';

preg_match_all('#<span class="num">(?:\s+)?(?P<num>\d(?:\.\d{2}):?)#isu', $html, $match);
var_dump($match['num']);
-1

Using regex.

$str = '<span class="paper-name">some string</span><div class="data-row"><span class="num">0.25';

preg_match('/([0-9]+\.?[0-9].)/', $str, $matches);

echo $matches[1];
onejeet
  • 1,191
  • 6
  • 14
  • Number must be between `0.00` and `9.99` your regex matches `1.2X`and matches numbers inside `abcd123456.123456xyz` – Toto Nov 27 '18 at 11:31