-2

how to create a regex to get numbers from a block online please tell me a good regex builder

    <div class="widget__header">
    <div class="stat-server-title">Players online:</div>
    <div class="stat-server-body"><b>2321</b></div>
    </div>
    <div class="right-stat-server season-stat-eng">
Anonymous
  • 835
  • 1
  • 5
  • 21
  • 1
    Don't use regex to parse HTML. It's a [bad idea](https://stackoverflow.com/a/1732454/8967612). But [why not?](https://stackoverflow.com/a/590789/8967612) Here are [some examples](https://stackoverflow.com/a/18724992/8967612) of problems you might run into. Use an [HTML parser](https://stackoverflow.com/q/3577641/8967612) instead. – 41686d6564 stands w. Palestine Jul 04 '22 at 00:05

2 Answers2

0

Not sure if this is what you want: but if you want to get the numbers from stat-server-body with a regex you can do: /<div.+class="stat-server-body"[^0-9]+([0-9]+)/

A little breakdown:

  1. Starting with <div to match any div start.
  2. Then everything in between until hitting class="stat-server-body"
  3. Then searching all non-numeric chars behind it till hitting the numbers
  4. And then grabbing all numbers behind it.

I like to use https://regex101.com/ for building/testing regexes, hope this helps.

WhiteFang
  • 199
  • 1
  • 10
0

If you want to get the number from the inner element it goes like this:

<b>(\d+)<\/b>

which the $1 (first group) will be the number 2321 regexr.com/6p0st

Nima Ghomri
  • 98
  • 1
  • 8