-3

I am new to regular expression.

The contents is

">blablabla</a> </td>blablabla "> 8.8GB </a> </td>

I want to get the value 8.8, but if i use (?<=">)(.*?)(?=GB<\/a>), it will get the texts before 8.8 as well, ie. ">blablabla</a> </td>blablabla "> 8.8GB

I don't know how to solve this. Any help is much appreciated.

Alan Moore
  • 73,866
  • 12
  • 100
  • 156
Wendy
  • 41
  • 5
  • 2
    If your string is html, use [`DOMDocument`](http://php.net/manual/en/domdocument.loadhtml.php) instead of regex – Mohammad Oct 06 '16 at 15:59
  • [The force of regex and HTML together in the same conceptual space will destroy your mind like so much watery putty.](http://stackoverflow.com/a/1732454/2370483) – Machavity Oct 06 '16 at 16:10
  • 2
    thank you chris85, it works perfectly! – Wendy Oct 06 '16 at 16:28

1 Answers1

1

You could use:

(\d+(?:\.\d+)?)[MKG]B

to extract the integer value before the storage unit.

The \d is any number.
+ is a quantifier allowing 1 or more numbers.
?: is a non-capturing group.
\. is a literal ..
? makes the decimal part (\.\d+) optional.
[MKG] is a character class allowing M, K, or G. B is a literal B.

Demo: https://regex101.com/r/3TifGy/1

PHP Usage:

<?php
preg_match('/(\d+(?:\.\d+)?)[MKG]B/', '">blablabla</a> </td>blablabla "> 8.8GB </a> </td>', $match);
echo $match[1];

PHP Demo: https://eval.in/656562

chris85
  • 23,846
  • 7
  • 34
  • 51