我如何从字符串的某一部分得到一个未知的数字


How can I get a unknown number from a string within a certain part of the string?

我的字符串:

How would you rate the ease and comfort required to undertake the session?@QUESTION_VALUE_0

我怎样才能从上面的字符串中得到这个值?我不知道这个值将是什么(除了它将是一个整数):

(some question)@QUESTION_VALUE_X,其中X是整数,我想得到X

我看了看Regex,但是我在正则表达式方面很糟糕,所以我很茫然,干杯!

关于正则表达式

/@QUESTION_VALUE_[0-9]+/

但是我无法从字符串中取出数字。我怎么能只抢到号码?

这应该可以为您工作:

只需将escape sequence 'd(这意味着0-9)与quantifier +(这意味着1或更多次)放入 (())以捕获您可以在数组$m中访问的数字。

<?php
    $str = "How would you rate the ease and comfort required to undertake the session?@QUESTION_VALUE_0";
    preg_match("/@QUESTION_VALUE_('d+)/", $str, $m);
    echo $m[1];
?>
输出:

0

如果你执行print_r($m);,你会看到你的数组结构:

Array
(
    [0] => @QUESTION_VALUE_0
    [1] => 0
)

现在你看到^,你在第一个元素中有完整的匹配,然后在第二个元素中有第一组(('d+))。