Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does space in square brakets have a different meaning than just space?

Tags:

regex

php

To give an example, does /[ ]{0,3}/ have a different meaning than / {0,3}/?

like image 235
Emanuil Rusev Avatar asked Apr 13 '13 13:04

Emanuil Rusev


2 Answers

It can have different meaning, if the x modifier flag to allow "free space" between tokens in the regex is enabled (for readability and to allow in-regex comments). But if this setting is not enabled, then there is no functional difference between the two.

Refer to this Regex Tutorial for a detailed explanation of this option in particular, where it states:

...free-spacing mode has no effect inside character classes. Spaces and line breaks inside character classes will be included in the character class.

Further, for PHP specific authority, refer The PHP manual for PCRE Pattern Modifiers which states:

x (PCRE_EXTENDED) If this modifier is set, whitespace data characters in the pattern are totally ignored except when escaped or inside a character class...

Refer also this other Regex Tutorial page on specifying modifiers that explains how to implement the modifier in different flavours of regex.

The modifier flag can be included in the regex itself as (?x) meaning free space is enabled from that point in the regex onwards to the end of the regex (or up to a closing (?-x) ). In particular for PHP, the modifier flag can be applied to the entire expression by trailing the regex eg (using one of your examples) / {0,3}/x

The square bracket format ([ ]) explicitly represents a literal space regardless of that setting, and IMO is self documenting; It very explicitly shows the devoloper's intention to use a literal space, perhaps in preference to the "\s" character class (which also includes tabs and new lines).

Further, and again just IMO, this is a preferable way to "escape" a space as a literal, as opposed to using "\ " which is easily misunderstood or overlooked by future code maintainers.

like image 149
Sepster Avatar answered Oct 16 '22 03:10

Sepster


They produce the same result, but the one in square brackets is more readable and more deliberate.

Personally I'd rather use \s unless a literal space is the only thing to allow.

like image 24
Niet the Dark Absol Avatar answered Oct 16 '22 01:10

Niet the Dark Absol