简体   繁体   English

我想使用正则表达式匹配字符串的第一个世界

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

I have a string like this: 我有一个像这样的字符串:

QUESTION3. 问题3。 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. R向北走35m,然后向右转并走20m,然后向右转并走25m。同样,他向右转并走20m。 How is point R from his starting point? R点从他的出发点如何?

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. 现在我要检查字符串的第一个字母是否以Question开头,然后是数字,然后是全句号(。),以便字符串以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 . [Question]是与单个字符Question匹配的字符类。 To match a sequence of digits, use \\d+ (1 or more). 要匹配数字序列,请使用\\d+ (1或更多)。 The i modifier makes the pattern case insenstive. i修饰符使模式不区分大小写。

Details : 详细资料

  • ^ - start of string ^ -字符串的开头
  • Question - a literal case insensitive (due to i ) substring Question -文字不区分大小写(由于i )子字符串
  • \\s* - 0+ whitespaces \\s* -0+空格
  • \\d+ - 1 or more digits \\d+ -1个或更多数字
  • \\s* - 0+ whitespaces \\s* -0+空格
  • \\. - a literal . -一个字面意思. (same as [.] but shorter). (与[.]相同,但更短)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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