Part of my regex is : /<a .*?(" "|"")(href)
. Within the (" "|"")
, I am trying to say match with either space or no space, but I can't get anything out of it. Ive also tried ("\s"|"")
with no results.

- 4,271
- 8
- 34
- 56

- 1,264
- 3
- 12
- 19
-
What are your trying to match exactly? Provide some input string and desired output string please. – Jerry Sep 09 '13 at 15:41
-
3Why don't you use `\s?`? – Moritz Petersen Sep 09 '13 at 15:41
-
2In the example pattern you give, are you explicitly looking to match the quote characters, or are you trying to use `" "` to mean a single space? – Bryan Oakley Sep 09 '13 at 15:50
2 Answers
"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*

- 370,779
- 53
- 539
- 685
-
1
-
"A single space or no space" is ` ?` and this is what I imagine the OP actually wants. So `/ – tripleee Sep 09 '13 at 15:48
-
I get your logic, but this is still not working. It only works when I actually put in the space (i.e. `/(href)`) as compared to your solution of `/' I actually mean a space character. – dudemanbearpig Sep 09 '13 at 16:00
-
@ImTryinl: can you please update your question to show an example of a line you're wanting to match against? `
*` should work anywhere ` – Bryan Oakley Sep 09 '13 at 17:26` works, so there must be something else going on. Can you also update your question to show what language you're using?
Other alternatives for the space
or no-space
problem (notice the spaces at the beginning in some):
[ ]{0,1}
{0,1}
[ ]?
?

- 1,096
- 16
- 29