0

I'm having issues with regex in PHP. Here is the code I am using the regex on:

<br />Level 163 Operative </div> <div class="sectionBar">

This is the preg_match statement:

preg_match("/<br \/>Level ([0-300]) (.*) <\/div>      <div class=\"sectionBar\"><\/div>/i", $output, $op);
  $top = $op[1];
  echo "$top";

However, when I try this, it doesn't echo anything.

Toto
  • 89,455
  • 62
  • 89
  • 125
Grant
  • 229
  • 5
  • 13
  • A [character class](http://www.regular-expressions.info/charclass.html) only matches a single character so you cannot use it for a range like `0-300` – Phil Nov 29 '13 at 00:41
  • **1st**, [please do NOT parse HTML with regex](http://stackoverflow.com/a/1732454/1519058)... **2nd**, Check [`THIS`](http://stackoverflow.com/a/3148251/1519058) for more information how character classes definition works... Then **finally**, check [`This too`](http://regex101.com/r/iV0xY0) – Enissay Nov 29 '13 at 01:00

2 Answers2

1

Here's one that matches 0-300; allows leading zero's, and specifically excludes the number if it is preceded by a negation sign; or embedded within other numbers:

'/(?<!-)\b0*(?:300|[12][0-9]{2}|[1-9]?[0-9])\b/'
Ron Rosenfeld
  • 53,870
  • 7
  • 28
  • 60
0

Here is a series of examples that should help you:

([1-3][0-9][0-9])

this will match 100-399, don't use [0-3] hoping to match below 100, as it'll only match numbers with a leading zero (055 but not 55)

([1-2]\d{2})

matches 100-299 (\d matches a single digit, same as [0-9])

([1-2]\d{2}|\d{2})

matches 10-299

([1-2]\d{2}|\d{2}|[1-9])

matches 1-299

([1-2]\d{2}|\d{2}|[1-9]|300)

matches 1-300

([1-2]?\d?\d|300)

1-300, more elegant way (making first 2 digits optional with ?)

([1-2]?\d?\d)

short version, matches 0-399

(\d{1-3})

shortest version, matches 0-999 (1-3 digits)

oldlol
  • 178
  • 2
  • 7