ACE-- 0 Light Poster

I need to get the values from below following html snippet. So far I came up with this regex which helps me trim it down to the values I needed, but to automate this I need to join 2 regex statements to get the result "18" which is where I am stuck at. Or Please suggest a better method for me get the values.

First Regex Statement

(?s)(?<=attribute bathroom).+?(?=\/span)

Result:

" title="Bathrooms" style="  ">
                    <span class="value" style="  ">18<

Second Regex Statement

(?s)(?<=<span class="value" style="  ">).+?(?=<)

Result:

18

HTML snippet

<ul class="iconContainer" style="  ">
                <li class="attribute propertyId">
                    <span class="value">xxx1</span>
                </li>
                <li class="attribute propertyType">
                    <span class="value">Factory</span>
                </li>
                <li class="attribute bathroom" title="Bathrooms" style="  ">
                    <span class="value" style="  ">18</span>
                </li>
                <li class="attribute carspace" title="Car Spaces" style="  ">
                    <span class="value" style=" ">18</span>
                </li>
                <li class="attribute landArea">
                        <span title="Land Area">
                            <span class="value">5,010</span><span class="m2"><span class="unit">m<b class="superscript">2</b></span></span>
                        </span>
                    <span>|</span>
                        <span title="Floor Area">
                            <span class="value">9,270</span><span class="m2"><span class="unit">m<b class="superscript">2</b></span></span>
                        </span>
                </li>
        </ul>

Dani AI

Generated

— chaining regex against HTML is brittle. The reliable approach is to parse the markup and use a CSS selector to pick the li with the bathroom class, then read the inner text of its span.value. That avoids fragile lookarounds, varying whitespace or extra attributes, and nested tags.

Browser JavaScript example:

const el = document.querySelector('li.attribute.bathroom span.value');
const value = el ? el.textContent.trim() : null;

Node.js with Cheerio:

const cheerio = require('cheerio');
const $ = cheerio.load(html);
const value = $('li.attribute.bathroom span.value').text().trim();

Python with BeautifulSoup:

from bs4 import BeautifulSoup
soup = BeautifulSoup(html, 'html.parser')
el = soup.select_one('li.attribute.bathroom span.value')
value = el.get_text(strip=True) if el else None

If you truly must use one regex, capture the inner text with a single pattern rather than two separate searches. For example:

/class=["']value["'][^>]*>\s*([^<]+)/

Group 1 will hold the visible text.

Practical tips: always trim and unescape HTML entities, and strip non-digit characters if you need a numeric type (remove commas, units). Check for missing elements (null in JS, None in Python) before accessing text. If the page can change, make selectors more tolerant (search the parent li for a title attribute or match class substrings). Prefer a parser for anything beyond one-off scraping — it is faster to write and far more robust than stacked regex.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.