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

标签 php regex

我有一个像这样的字符串:

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?

现在我想检查字符串的第一个字母是否以 Question 开头,然后是数字,然后是句号(.),因此字符串将以 Question244 开头。我正在使用

if(preg_match('/^[Question][0-9 ]{0,4}[.]/', $value)){
  echo "is a qeustion";
}else{
  echo "Not a question";
}

表达式但不起作用,

最佳答案

您可以使用

/^Question\s*\d+\s*\./i

请参阅regex demo

[Question] 是一个匹配单个字符的字符类,可以是 Question。要匹配数字序列,请使用 \d+ (1 个或多个)。 i 修饰符使模式不区分大小写。

详细信息:

  • ^ - 字符串开头
  • 问题 - 不区分大小写的子字符串(由于i)子字符串
  • \s* - 0+ 个空格
  • \d+ - 1 位或更多数字
  • \s* - 0+ 个空格
  • \. - 文字 .(与 [.] 相同,但更短)。

关于php - 我想使用正则表达式匹配字符串的第一个世界,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45103471/

相关文章:

PHP7 cli 与 php-fpm

javascript - AJAX POST 不起作用

javascript - 提交表单而不刷新页面

java - 从正则表达式输出中删除空字符串

C++:正则表达式模式

regex - 使用 grep 查找具有超过 1 个匹配项的文件

javascript - jquery如何减去元素

javascript - 如何从 db laravel 6 将事件类添加到菜单

regex - 为什么我会收到意想不到的 token ?

python unicode 正则表达式