Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4608672
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:52:42+00:00 2026-05-22T00:52:42+00:00

I understand since Regex is essentially stateless, it’s rather difficult to achieve complicated matches

  • 0

I understand since Regex is essentially stateless, it’s rather difficult to achieve complicated matches without resorting to supplementing application logic, however I’m curious to know if the following is possible.

Match all whitespace, easy enough: \s+

But skip whitespace between certain delimiters, in my case <pre> and </pre> the word nostrip.

Are there any tricks to achieve this? I was thinking along the lines of two separate matches, one for all whitespace, and one for <pre> blocks nostrip sections, and somehow negating the latter from the former.

"This is some text NOSTRIP this is more text NOSTRIP some more text."
// becomes
"ThisissometextNOSTRIP this is more text NOSTRIPsomemoretext."

The nesting of given tags nostrip sections is irrelevant, and I’m not trying to parse the tree HTML or anything, just tidying a text file, but saving the whitespace in <pre> blocks nostrip sections for obvious reasons.

(better?)


This is ultimately what I went with. I’m sure it can be optimized in a few places, but it works nicely for now.

public function stripWhitespace($html, Array $skipTags = array('pre')){
    foreach($skipTags as &$tag){
        $tag = "<{$tag}.*?/{$tag}>";
    }
    $skipped = array();
    $buffer = preg_replace_callback('#(?<tag>' . implode('|', $skipTags) . ')#si',
        function($match) use(&$skipped){
            $skipped[] = $match['tag'];
            return "\x1D" . (count($skipped) - 1) . "\x1D";
        }, $html
    );
    $buffer = preg_replace('#\s+#si', ' ', $buffer);
    $buffer = preg_replace('#(?:(?<=>)\s|\s(?=<))#si', '', $buffer);
    for($i = count($skipped) - 1; $i >= 0; $i--){
        $buffer = str_replace("\x1D{$i}\x1D", $skipped[$i], $buffer);
    }
    return $buffer;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T00:52:43+00:00Added an answer on May 22, 2026 at 12:52 am

    I you are using a scripting language, I would use a multi-step approach.

    • pull out the NOSTRIP sections, and save to an array, and replace with markers (### or something)
    • replace all the spaces
    • re-inject all your saved NOSTRIP snippets
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I understand SetCurrentDirectory shouldn't be used in a multithreaded application since the current directory
Edit Since there were many downvotes and people who didn't understand what I'm asking
I understand WURFL changed their license to AGPL since 2.2. On their website I
Gonna give an example since it's hard to understand what i'm talking about: SELECT
I understand that Web Site Projects compile source on-the-fly, and Web Application Projects pre-compile
I understand the pros of using namespaces within the actual application layers but when
There's an error message I can't understand since I don't see where my code
In my Python application, I need to write a regular expression that matches a
Okay, I barely understand RegEx basics, but why couldn't they design it to use
(I have tagged this question as Python as well since I understand Python code

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.