Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Space or no space

Tags:

regex

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.

like image 312
dudemanbearpig Avatar asked Sep 09 '13 15:09

dudemanbearpig


People also ask

Is it space or no space?

As a general rule, in English there is no space before and one space after a punctuation mark. Exceptions follow.

Should you put a space after a slash?

If the slash divides two words, there is no space. If it divides two phrases or sentences (or a single word from a phrase), it requires a space before and after. Please see CMOS 6.106.

Does and or have a space?

Do you leave spaces around slashes in expressions such as “and/or” or do you open up spaces, using “and / or”? I've seen both styles. The online AP Stylebook (paid subscription required) says “No space on either side of the slash.”

What does the slash symbol mean?

The slash is an oblique slanting line punctuation mark /. Once used to mark periods and commas, the slash is now used to represent exclusive or inclusive or, division and fractions, and as a date separator.


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*

like image 117
Bryan Oakley Avatar answered Sep 22 '22 15:09

Bryan Oakley


Other alternatives for the space or no-space problem (notice the spaces at the beginning in some):

[ ]{0,1}  {0,1} [ ]?  ? 
like image 39
JaKu Avatar answered Sep 21 '22 15:09

JaKu