当多个值可能不存在时,正则表达式匹配多个值
我正在尝试纠正 preg_match_all 以匹配赛马距离。
我的资料来源将种族列为: xmxfxy 我想匹配 m 值、f 值、y 值。然而,不同的种族可能只有m,或f,或y,或其中两个,甚至全部三个。
// e.g. $raw = 5f213y;
preg_match_all('/(\d{1,})m|(\d{1,})f|(\d{1,})y/', $raw, $distance);
上面的排序是有效的,但由于某种原因,匹配项出现在返回数组中不可预测的位置。我猜这是因为它为每个 OR 运行了 3 次匹配。如何在一次运行中匹配所有三个(可能存在或不存在)。
编辑 完整的示例字符串是:
Hardings Catering Services Handicap (Div I) Cl6 5f213y
I am trying to right a preg_match_all to match horse race distance.
My source lists races as:
xmxfxy
I want to match the m value, the f value, the y value. However different races will maybe only have m, or f, or y, or two of them or even all three.
// e.g. $raw = 5f213y;
preg_match_all('/(\d{1,})m|(\d{1,})f|(\d{1,})y/', $raw, $distance);
The above sort of works, but for some reason the matches appear in unpredictable positions in the returned array. I guess it is because it is running the match 3 times for each OR. How do I match all three (that may or may not exist) in a single run.
EDIT
A full sample string is:
Hardings Catering Services Handicap (Div I) Cl6 5f213y
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果我理解正确的话,您正在一次处理一个列表(如您问题中的列表)。如果是这种情况,您应该使用
preg_match
,而不是preg_match_all
,并且正则表达式应该匹配整个“距离”代码,而不是其中的各个组件。试试这个:结果现在存储在一个一维数组中,但无论如何你都不需要担心组号;您可以通过名称来访问它们(例如,
$distance['M']
、$distance['F']
、$distance['Y'] )。
请注意,虽然此正则表达式匹配具有一个、两个或三个组成部分的代码,但它不要求字母是唯一的。没有什么可以阻止它匹配像
1m2m3m
这样的东西(顺便说一句,这是你自己的方法所共有的弱点)。If I understand you correctly, you're processing listings (like the one in your question) one at a time. If that's the case, you should be using
preg_match
, notpreg_match_all
, and the regex should match the whole "distance" code, not individual components of it. Try this:The results are now stored in a one-dimensional array, but you don't need to worry about the group numbers anyway; you can access them by name instead (e.g.,
$distance['M']
,$distance['F']
,$distance['Y']
).Note that, while this regex matches codes with one, two, or three components, it doesn't require the letters to be unique. There's nothing to stop it from matching something like
1m2m3m
(a weakness shared by your own approach, by the way).你可以使用“?”作为有条件的
you can use "?" as a conditional
如果我正确理解您的要求,您想分别从这些值中获取每个数字吗?这对我有用:
在
preg_match_all()
执行之后,$matches[2]
保存一个匹配数字的数组(在本例中,$matches[ 2][0]
为 5,$matches[2][1]
为 213。如果这三个值都存在,则 m 将位于
$matches[2][0 中]
、$matches[2][1]
中的 f 和$matches[2][2]
中的 y 如果缺少任何值,则下一个值会上升一个位置,$matches[3]
将保存匹配的相应字母的数组,因此如果您需要检查它是否是 m, f 或 y,如果这不是您想要的,请提供您希望看到的此或另一个示例输入的输出示例。
If I understand what you're asking correctly, you would like to get each number from these values separately? This works for me:
After the
preg_match_all()
executes,$matches[2]
holds an array of the numbers that matched (in this case,$matches[2][0]
is 5 and$matches[2][1]
is 213.If all three values exist, m will be in
$matches[2][0]
, f in$matches[2][1]
, and y in$matches[2][2]
. If any values are missing, the next value gets bumped up a spot. It may also come in handy that$matches[3]
will hold an array of the corresponding letter matched on, so if you need to check whether it was an m, f, or y, you can.If this isn't what you're after, please provide an example of the output you would like to see for this or another sample input.