Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 正则表达式帮助。捕获以“开始”开头的字符串;分数“;以数字结尾_Regex_Preg Match - Fatal编程技术网

Regex 正则表达式帮助。捕获以“开始”开头的字符串;分数“;以数字结尾

Regex 正则表达式帮助。捕获以“开始”开头的字符串;分数“;以数字结尾,regex,preg-match,Regex,Preg Match,在这篇文章之前我已经尝试了很多,但是simple无法让它发挥作用,我需要你的帮助 想象以下字符串: $str = "Games >= 2 AND score >= 30 and country = 2"; 或 或 或 或 或 有我的正则表达式 $re = '/(score.*\d\d|score.*\d)/mi'; preg_match($re, one_of_those_strings_above, $matches, PREG_OFFSET_CAPTURE, 0);

在这篇文章之前我已经尝试了很多,但是simple无法让它发挥作用,我需要你的帮助

想象以下字符串:

$str = "Games  >=  2  AND score  >= 30 and country = 2";

有我的正则表达式

$re = '/(score.*\d\d|score.*\d)/mi';

preg_match($re, one_of_those_strings_above, $matches, PREG_OFFSET_CAPTURE, 0);

var_dump($matches);
你可以在这里查一下

我可以捕获我想要的,但是如果数字小于10(在示例中为2到9),正则表达式将失败


非常感谢。

看来我们需要更具体一些

(score\s*(?:[><]?=|between)\s*\d+(?:\s+and\s+\d+)?)
(分数*(?:[>=
试试:

/score.*?

  • score
    匹配
    score
  • *
    一种贪婪的、最大匹配的0个或多个非换行符。如果需要
    匹配换行符,请使用标志
    s
  • (?将一个或两个数字与声明进行匹配,声明它们的前面或后面没有任何其他数字。因此将执行最大匹配,直到找到一个或两个数字

  • 你能提供一些正则表达式无法正常工作的例子吗?@ggorlen我已经更新了帖子,并在regex101中提供了一个示例。com@varontron我已经更新了帖子,并在regex101.com中提供了一个示例,但它不起作用,因为如果游戏>=2,并且分数在2到2之间,Pais=2,那么它将抓取2到9之间的Pais=2根据分数将正则表达式更新为Account,但如果分数在1到9之间,则不起作用。仅适用于大于10的数字。示例:分数在1到20之间。我尝试了您的代码,但代码对我来说不正常。因此,我再次更新了您的逻辑。
    /(分数\s*(?:[>非常感谢大家。
    
    $str = "Games  >=  2  AND score  between 10 and 60 and country = 2";
    
    $str = "score  between 10 and 20 and Games  >=  2";
    
    $str = "score  between 2 and 9 and Games  >=  2";
    
    $str = "Games  >=  2  AND score = 3";
    
    $re = '/(score.*\d\d|score.*\d)/mi';
    
    preg_match($re, one_of_those_strings_above, $matches, PREG_OFFSET_CAPTURE, 0);
    
    var_dump($matches);
    
    (score\s*(?:[><]?=|between)\s*\d+(?:\s+and\s+\d+)?)
    
    /score.*(?<!\d)\d\d?(?!\d)/