PHP正则表达式括号内的精确数据
我有一些像西澳大利亚223/559.3OV的弦 我想拆分这个字符串并用正则表达式提取以下信息PHP正则表达式括号内的精确数据,php,regex,Php,Regex,我有一些像西澳大利亚223/559.3OV的弦 我想拆分这个字符串并用正则表达式提取以下信息 $team = 'Western Australia' $runs = 223/5 $overs = 59.3 问题是,文本的格式是不同的,它可能有以下任何一种 西澳大利亚223/5 59.3 ov 澳大利亚223/5 59.3 ov 夸祖鲁-纳塔尔内陆酒店 斯里兰卡诉西印度群岛 如能在单个regexp中提供任何帮助,我们将不胜感激。假设您使用的是preg_match,您可以使用以下内容: preg_
$team = 'Western Australia'
$runs = 223/5
$overs = 59.3
问题是,文本的格式是不同的,它可能有以下任何一种
西澳大利亚223/5 59.3 ov
澳大利亚223/5 59.3 ov
夸祖鲁-纳塔尔内陆酒店
斯里兰卡诉西印度群岛
如能在单个regexp中提供任何帮助,我们将不胜感激。假设您使用的是preg_match,您可以使用以下内容:
preg_match('/^([\w\s]+)\s*(\d+\/\d+)?\s*(\(\d+\.\d+ ov\))?$/', $input, $matches);
然后,您可以检查$matches,以查看找到了支持您管理的选项之一
有关更多信息,请参阅。假设您使用的是preg_match,则可以使用以下选项:
preg_match('/^([\w\s]+)\s*(\d+\/\d+)?\s*(\(\d+\.\d+ ov\))?$/', $input, $matches);
if (preg_match(
'%^ # start of string
(?P<team>.*?) # Any number of characters, as few as possible (--> team)
(?:\s+ # Try to match the following group: whitespace plus...
(?P<runs>\d+ # a string of the form number...
(?:/\d+)? # optionally followed by /number
) # (--> runs)
)? # optionally
(?:\s+ # Try to match the following group: whitespace plus...
\( # (
(?P<overs>[\d.]+) # a number (optionally decimal) (--> overs)
\s+ov\) # followed by ov)
)? # optionally
\s* # optional whitespace at the end
$ # end of string
%six',
$subject, $regs)) {
$team = $regs['team'];
$runs = $regs['runs'];
$overs = $regs['overs'];
} else {
$result = "";
}
然后,您可以检查$matches,以查看找到了支持您管理的选项之一
有关更多信息,请参阅
if (preg_match(
'%^ # start of string
(?P<team>.*?) # Any number of characters, as few as possible (--> team)
(?:\s+ # Try to match the following group: whitespace plus...
(?P<runs>\d+ # a string of the form number...
(?:/\d+)? # optionally followed by /number
) # (--> runs)
)? # optionally
(?:\s+ # Try to match the following group: whitespace plus...
\( # (
(?P<overs>[\d.]+) # a number (optionally decimal) (--> overs)
\s+ov\) # followed by ov)
)? # optionally
\s* # optional whitespace at the end
$ # end of string
%six',
$subject, $regs)) {
$team = $regs['team'];
$runs = $regs['runs'];
$overs = $regs['overs'];
} else {
$result = "";
}
如果字符串中实际不存在匹配项和/或,则可能需要捕获错误。我对PHP了解不多。我不太懂生物学…SCNR
如果字符串中实际不存在匹配项和/或,则可能需要捕获错误。我对PHP了解不多。我不太懂生物学……SCNR在后两种情况下,预期的输出是什么?BoltClock告诉了我。是的…需要更多信息!我不确定后两个的输出应该是什么!!:-它可以是完整的字符串本身。我不是在关注输出的意义,而是在处理regexp时尽量不出错。在后两种情况下,预期的输出是什么?BoltClock告诉了我。是的…需要更多信息!我不确定后两个的输出应该是什么!!:-它可以是完整的字符串本身。我不是在关注输出的意义,而是在处理regexpsWon时尽量不出错。西澳大利亚是两个字,不是一个。而且,它不会匹配ov位周围的s。不会工作。西澳大利亚是两个字,不是一个。而且,它不会匹配ov位周围的s。哦!!!西澳州324 OK的最新分数表达式失败,因此/number部分是可选的?没问题。Will edit.number/number部分是可选的,可能的情况可以是西澳州、西澳州1/0、西澳州324/9、西澳州324oops!!!西澳州324 OK的最新分数表达式失败,因此/number部分是可选的?没问题。将编辑。编号/编号部分是可选的,可能的情况可以是西澳大利亚州、西澳大利亚州1/0、西澳大利亚州324/9、西澳大利亚州324