I’m using preg_replace in PHP and trying to match any number of whitespaces between two words specifically between wordA and wordB.
Here’s my current code:
$verif = "/wordA(\s*)wordB(?! wordc)/i";
$replacement = 'wordA wordb wordc';
$newvar = preg_replace($verif, $replacement, $article->text);
This works fine when there’s exactly one space or maybe two between wordA and wordB, but I want it to work regardless of how many spaces or tabs are between them even 10 or more. I also want to avoid a match if wordC follows.
What’s the correct regex whitespace pattern that handles multiple space characters reliably with preg_replace in this context?
Hey, I ran into a similar issue a while back. After some trial and error, I found that using \s+
works wonders. This ensures that at least one whitespace character (spaces, tabs, etc.) is present between words.
It catches even multiple whitespace characters. Also, you might want to tweak your negative lookahead to avoid trailing whitespace before wordC
. Here’s the regex I used:
$verif = "/wordA\s+wordB(?!\s*wordc)/i";
This way, it’ll catch wordA
followed by any amount of whitespace before wordB
, but it won’t match if wordC
is right after. It saved me a lot of trouble when I was parsing content with irregular spaces.
Ah, totally get you! I faced the same issue while building a markdown-to-HTML converter. Inconsistent spacing across inputs was a pain until I got smarter with regex patterns. Using \s+
grabs one or more whitespace characters (including tabs, newlines, or spaces), and (?!\s*wordc)
is a handy negative lookahead to skip unwanted matches. Here’s the pattern I settled on:
$verif = "/wordA\s+wordB(?!\s*wordc)/i";
This ensures the regex consistently handles messy input across various whitespace types. It’s like magic for input sanitization!
Yep, same experience here. I found that relying solely on spaces isn’t ideal for real-world cases. For me, the key was using \s+
in the regex. It worked great when paired with preg_replace
to clean up messy text, whether it’s tabs, spaces, or even newlines. Here’s the final pattern I used for replacing:
$verif = "/wordA\s+wordB(?!\s*wordc)/i";
$replacement = 'wordA wordB wordC';
$newvar = preg_replace($verif, $replacement, $article->text);
This solution has been solid for handling those edge cases like when someone accidentally pastes in 10 tabs. It’s consistent, and you won’t regret using it!