"space or no space" is the same as "zero or one space", or perhaps "zero or more spaces", I'm not sure exactly what you want.

In the following discussion, I'm going to use <space> to represent a single space, since a single space is hard to see in short code snippets. In the actual regular expression, you must use an actual space character.

zero-or-one-space is represented as a single space followed by a question mark (<space>?). That will match exactly zero or one spaces. If you want to match zero or any number of spaces, replace the ? with * (eg: <space>*)

If by "space" you actually mean "any whitespace character" (for example, a tab), you can use \s which most regular expression engines translate as whitespace. So, zero-or-one of any whitespace character would be \s?, and zero-or-more would be \s*

Answer from Bryan Oakley on Stack Overflow
Discussions

Regex Zero or More Spaces Within a Group of Digits - Stack Overflow
suppose I have a sequence of one to three digits that can have any number of spaces in between them, and suppose that these numbers are within a group that I can back reference. How would I go by d... More on stackoverflow.com
🌐 stackoverflow.com
November 28, 2016
regular expression - sed to match zero or more number of spaces in a string - Unix & Linux Stack Exchange
In that case, the function fails. I tried with tweaking the above pattern by adding [ ] to match zero or more spaces as, More on unix.stackexchange.com
🌐 unix.stackexchange.com
December 26, 2015
java - Regex for ONE-or-more letters/digits And ZERO-or-more spaces - Stack Overflow
I want to allow 0 or more white spaces in my string and one or more A-Z or a-z or 0-9 in my string. Regex allowing a space character in Java suggests [0-9A-Za-z ]+. I doubt that, this regex matc... More on stackoverflow.com
🌐 stackoverflow.com
July 24, 2019
allow for zero or more spaces in regex with grep - Stack Overflow
I have some files which may look like mem = 500, mem= 100, mem =256, or any combination of mem, whitespaces, =, and a number. If I assume there is no whitespace I can use: grep -oP '(? More on stackoverflow.com
🌐 stackoverflow.com
🌐
TextPad Community
forums.textpad.com › home › board index › peer group support › general
Matching 0 or more spaces in searches - Community - TextPad
August 18, 2023 - Especially codes and quotations ... based on the ones used at that time by grep in Unix. Zero or more horizontal space characters can be matched using "[ \t]*"....
🌐
Softhints
softhints.com › regex-match-no-space-or-one-space-python
Regex to Match no space or one space in Python
February 10, 2022 - If so, you may use the following syntax to match similar patterns: * [ ]{0,1} - match no space or 1 space * [-]? - match nothing or a single hyphen Let's demonstrate usage of them with
Find elsewhere
🌐
Position Is Everything
positioniseverything.net › home › regex whitespace: how to handle whitespace in strings
Regex Whitespace: How To Handle Whitespace in Strings - Position Is Everything
December 29, 2025 - You can also use the \s character class in conjunction with other regular expression constructs, such as the * and + operators, to match zero or more, or one or more, occurrences of whitespace characters, ...
🌐
Coderanch
coderanch.com › t › 432220 › certification › Regular-Expression-match-leding-whitespace
Regular Expression to match zero or more leding whitespace characters (OCPJP forum at Coderanch)
February 20, 2009 - Hi all, testing out the Pattern and Matcher functionality I came up with this example: Running it yields, as expected Pattern='(^\s*)([\$_A-Za-z][A-za-z0-9]*)(\s*=\s*\1)' Target=' name = name;' After applying replaceFirst '$1this.$2$3': this.name = name; However, inserting a second leading space or tab into the String s causes "No match" to be printed. Can anybody explain why? ... At the end of your regex, maybe you meant \\2 instead of \\1.
🌐
RegexOne
regexone.com › lesson › whitespaces
RegexOne - Learn Regular Expressions - Lesson 9: All this whitespace
The most common forms of whitespace you will use with regular expressions are the space (␣), the tab (\t), the new line (\n) and the carriage return (\r) (useful in Windows environments), and these special characters match each of their respective whitespaces.
🌐
Reddit
reddit.com › r/csharp › avoiding all the white space using regex
Avoiding all the white space using RegEx : r/csharp
February 12, 2025 - You've got your regex answer below. But for something this simple you can also just use string.Split and tell it to RemoveEmptyEntries as an override as long as the source system isn't strangely sending different whitespace chars ... \s+ will match one or more whitespace characters. Use this instead of the actual spaces ...
🌐
RegExr
regexr.com › 4s7hp
RegExr: Learn, Build, & Test RegEx
Supports JavaScript & PHP/PCRE RegEx. Results update in real-time as you type. Roll over a match or expression for details.
🌐
TutorialsPoint
tutorialspoint.com › article › how-to-write-python-regular-expression-to-get-zero-or-more-occurrences-within-the-pattern
How to write Python regular expression to get zero or more occurrences within the pattern?
March 24, 2026 - import re # Define a string to search input_string = "aaa bbb cc d eee" # Use regex pattern for zero or more 'a' pattern = r'a*' # Find all matches matches = re.findall(pattern, input_string) print("Matches found:", matches) ... In this example, we use \s* to match zero or more whitespace characters. This is useful for handling variable spacing in text ? import re # Define a string with various spaces input_string = "Hello World" # Use regex to match zero or more whitespace pattern = r'\s*' # Find all matches matches = re.findall(pattern, input_string) print("Whitespace matches:", matches) # More practical example - splitting by multiple spaces words = re.split(r'\s+', input_string) print("Words:", words)
🌐
NTU Singapore
www3.ntu.edu.sg › home › ehchua › programming › howto › Regexe.html
Regular Expression (Regex) Tutorial
This regex matches any non-empty numeric strings (comprising of digits 0 to 9), e.g., "0" and "12345". It does not match with "" (empty string), "abc", "a123", "abc123xyz", etc. However, it also matches "000", "0123" and "0001" with leading zeros. [1-9] matches any character between 1 to 9; [0-9]* ...
Top answer
1 of 2
70

Your regex should work 'as-is'. Assuming that it is doing what you want it to.

wordA(\s*)wordB(?! wordc)

This means match wordA followed by 0 or more spaces followed by wordB, but do not match if followed by wordc. Note the single space between ?! and wordc which means that wordA wordB wordc will not match, but wordA wordB wordc will.

Here are some example matches and the associated replacement output:

Note that all matches are replaced no matter how many spaces. There are a couple of other points: -

  • (?! wordc) is a negative lookahead, so you wont match lines wordA wordB wordc which is assume is intended (and is why the last line is not matched). Currently you are relying on the space after ?! to match the whitespace. You may want to be more precise and use (?!\swordc). If you want to match against more than one space before wordc you can use (?!\s*wordc) for 0 or more spaces or (?!\s*+wordc) for 1 or more spaces depending on what your intention is. Of course, if you do want to match lines with wordc after wordB then you shouldn't use a negative lookahead.

  • * will match 0 or more spaces so it will match wordAwordB. You may want to consider + if you want at least one space.

  • (\s*) - the brackets indicate a capturing group. Are you capturing the whitespace to a group for a reason? If not you could just remove the brackets, i.e. just use \s.

Update based on comment

Hello the problem is not the expression but the HTML out put   that are not considered as whitespace. it's a Joomla website.

Preserving your original regex you can use:

wordA((?:\s|&nbsp;)*)wordB(?!(?:\s|&nbsp;)wordc)

The only difference is that not the regex matches whitespace OR &nbsp;. I replaced wordc with \swordc since that is more explicit. Note as I have already pointed out that the negative lookahead ?! will not match when wordB is followed by a single whitespace and wordc. If you want to match multiple whitespaces then see my comments above. I also preserved the capture group around the whitespace, if you don't want this then remove the brackets as already described above.

Example matches:

2 of 2
2

The reason I used a + instead of a '*' is because a plus is defined as one or more of the preceding element, where an asterisk is zero or more. In this case we want a delimiter that's a little more concrete, so "one or more" spaces.

word[Aa]\s+word[Bb]\s+word[Cc]

will match:

wordA wordB     wordC
worda wordb wordc
wordA   wordb   wordC

The words, in this expression, will have to be specific, and also in order (a, b, then c)