简体   繁体   中英

I want to match the first world of the string using regular expression

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 string
  • Question - 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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM