I have a string like this:
QUESTION3. R walks 35m to the north, then he turns right and walks 20m, then he turns to his right and walks 25m.Again, he turns to his right and walks 20m. How is point R from his starting point?
Now I wanna check if the first letter of the string starts with Question then the number and then full stop(.) so the string will start will Question244. I am using
if(preg_match('/^[Question][0-9 ]{0,4}[.]/', $value)){
echo "is a qeustion";
}else{
echo "Not a question";
}
expression but it is not working,
You may use
/^Question\s*\d+\s*\./i
See the regex demo
The [Question]
is a character class that matches a single char, either Q
, u
, e
, s
, t
, i
, o
or n
. To match a sequence of digits, use \d+
(1 or more). The i
modifier makes the pattern case insenstive.
Details:
^
- start of stringQuestion
- a literal case insensitive (due to i
) substring\s*
- 0+ whitespaces\d+
- 1 or more digits\s*
- 0+ whitespaces\.
- a literal .
(same as [.]
but shorter).