我有一个可以用不同方式看的字符串。
$str = "11:00 Team1 - Team2 0-0"
$str = "4' Team1 - Team2 2-1"
$str = "Half time Team1 - Team2 2-1"
$str = "55' Team1 - Team2 3-1"
$str = "Finished Team1 - Team2 2-1"
通过使用正则表达式,我想知道字符串何时包含“Number +:+ Number”,“Number +'”以及前两个组合中的任何一个。此外,我想提取“数字+ - +数字”。
我在第二个例子中试过这个:
preg_match("/[0-9+']/", $str)
答案 0 :(得分:1)
这些是您需要的正则表达式:
对于第一个:(数字+:+数字)
/[0-9]+:[0-9]+/
对于第二个:(数字+')
/[0-9]+'/
第三个:(数字+ - +数字)
/[0-9]+-[0-9]+/
答案 1 :(得分:0)
$ret = preg_match_all('/^([0-9]+\'|:)/', $str, $matches);
var_dump($ret) // 0 for no match, 1 for match
答案 2 :(得分:0)
匹配所有案例的正确表达式将是
\d+[:'-]\d*
它匹配一个或多个数字,然后是冒号,'或破折号,然后是零或更多数字。
PHP示例
$str[] = "11:00 Team1 - Team2 0-0";
$str[] = "4' Team1 - Team2 2-1";
$str[] = "Half time Team1 - Team2 2-1";
$str[] = "55' Team1 - Team2 3-1";
$str[] = "Finished Team1 - Team2 2-1";
foreach($str as $s) {
preg_match_all("/\d+[:'-]\d*/", $s, $res);
print_r($res);
}
打印
Array ( [0] => Array ( [0] => 11:00 [1] => 0-0 ) )
Array ( [0] => Array ( [0] => 4' [1] => 2-1 ) )
Array ( [0] => Array ( [0] => 2-1 ) )
Array ( [0] => Array ( [0] => 55' [1] => 3-1 ) )
Array ( [0] => Array ( [0] => 2-1 ) )