Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does [\S\s]* mean in regex in PHP?

Tags:

regex

php

What is meant by [\s\S]* in regex in PHP? Does [\s\S]* actually match every string the same as .*?

like image 578
yoyo Avatar asked Dec 28 '10 08:12

yoyo


People also ask

What does this mean in regex \\ s *?

\\s*,\\s* It says zero or more occurrence of whitespace characters, followed by a comma and then followed by zero or more occurrence of whitespace characters. These are called short hand expressions. You can find similar regex in this site: http://www.regular-expressions.info/shorthand.html.

What is S and W in regex?

On the other hand, the \S+ (uppercase S ) matches anything that is NOT matched by \s , i.e., non-whitespace. In regex, the uppercase metacharacter denotes the inverse of the lowercase counterpart, for example, \w for word character and \W for non-word character; \d for digit and \D or non-digit.

What does regex 0 * 1 * 0 * 1 * Mean?

Basically (0+1)* mathes any sequence of ones and zeroes. So, in your example (0+1)*1(0+1)* should match any sequence that has 1. It would not match 000 , but it would match 010 , 1 , 111 etc. (0+1) means 0 OR 1.

What does regex (? S match?

i) makes the regex case insensitive. (? s) for "single line mode" makes the dot match all characters, including line breaks.


2 Answers

By default . doesn't match new lines - [\s\S] is a hack around that problem.
This is common in JavaScript, but in PHP you can use the /s flag to to make the dot match all characters.

like image 165
Kobi Avatar answered Oct 22 '22 13:10

Kobi


The . meta character matches any character except a newline. So the pattern .* which is used to match anything will not work if you have to match newlines as-well.

preg_match('/^.*$/',"hello\nworld"); // returns 0

[\s\S] which is a character class of white-space characters and non-whitespace characters matches any character including a newline so do [\d\D], [\w\W]. So your pattern [\s\S]* now matches anything.

preg_match('/^[\s\S]$/s',"hello\nworld"); // returns 1

An alternative to make . match anything (including a newline) is to use a s modifier.

preg_match('/^.*$/s',"hello\nworld"); // returns 1 

Alternative way of using the s modifier is in-lining it as:

preg_match('/^(?s).*(?-s)$/',"hello\nworld"); // returns 1

(?s) turns on the s mode and (?-s) turns if off. Once turned off any following . will not match a newline.

like image 42
codaddict Avatar answered Oct 22 '22 14:10

codaddict