我的字符串:
How would you rate the ease and comfort required to undertake the session?@QUESTION_VALUE_0
我如何才能从上面的字符串中具体获取此值?我不知道这个值是什么(除了它将是一个整数):
(some question)@QUESTION_VALUE_X
X是整数,我想得到X。
我看着正则表达式,但我很讨厌正则表达式,所以我很茫然,欢呼!
关于正则表达式
/@QUESTION_VALUE_[0-9]+/
但是我无法从字符串中取出数字。我怎么只能拿这个号码?
这应该为您工作:
只需将\ d(表示0-9)和+(表示1次或多次)放入组(())中,以捕获随后可以在数组中访问的数字。escape sequence
quantifier
$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 +))。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句