php REGEX,尝试从一行中提取两个值(一个可选)
我有一个包含很多行的字符串,其中一行是温度读数以及用于获取温度的方法,如下所示:
Example line 1
temp: 35.20c / 95.36f - axillary
Example line 2
显然温度是“35.20c / 95.36f”,方法是“腋窝”。方法部分是可选的。我在编写提取两者的 REGEX 模式时遇到问题,因为该方法是可选的。
因此,如果我在以下字符串上运行 preg_match_all() 中的模式:
temp: 35.20c / 95.36f - axillary
temp: 35.20c / 95.36f
temp: 35.20c / 95.36f - oral
我希望得到与此类似的打印:
Array
(
[0] => Array
(
[0] => temp: 35.20c / 95.36f - axillary
[1] => temp: 35.20c / 95.36f
[2] => temp: 35.20c / 95.36f - oral
)
[1] => Array
(
[0] => 35.20c / 95.36f
[1] => 35.20c / 95.36f
[2] => 35.20c / 95.36f
)
[2] => Array
(
[0] => axillary
[1] =>
[2] => oral
)
我已经尝试了许多不同的模式,所以我只发布我的原始模式(这对我来说很有意义):
$ptn = "/temp: *(.+)(?: - )?(.+)?/";
抱歉,我想我需要添加更多细节:
- 我不知道温度将以何种格式显示(35.20c / 95.36f、35c、95.3f 等),
- 我基本上只需要在“ temp: " 在连字符之前作为我的 temp,之后的所有内容都将是我的方法。
I have a string with many lines and one of those lines is a temperature reading and the method used to take the temperature like so:
Example line 1
temp: 35.20c / 95.36f - axillary
Example line 2
Obviously the temp is "35.20c / 95.36f", and the method is "axillary". The method part is optional. I'm having problems writing a REGEX pattern that will extract both since the method can be optional.
So if i run the pattern in a preg_match_all() on the following string:
temp: 35.20c / 95.36f - axillary
temp: 35.20c / 95.36f
temp: 35.20c / 95.36f - oral
I would expect to get a print similar to this:
Array
(
[0] => Array
(
[0] => temp: 35.20c / 95.36f - axillary
[1] => temp: 35.20c / 95.36f
[2] => temp: 35.20c / 95.36f - oral
)
[1] => Array
(
[0] => 35.20c / 95.36f
[1] => 35.20c / 95.36f
[2] => 35.20c / 95.36f
)
[2] => Array
(
[0] => axillary
[1] =>
[2] => oral
)
I have tried many different patterns, so I'll just post my original (which makes sense to me):
$ptn = "/temp: *(.+)(?: - )?(.+)?/";
Sorry guys I guess I need to add some more details:
- I have no idea what kind of format the temp will be displayed in (35.20c / 95.36f, 35c, 95.3f, etc)
- I basically just need to take everything after the "temp: " and before the hyphen as my temp and everything after that is going to be my method.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
试试这个:
Try this one:
啊,我认为你的问题是 (.+) 匹配所有内容。正则表达式模式是“贪婪的”,并且会尝试尽可能多地匹配。该模式与字符串的其余部分匹配,不为其他组留下任何内容。
Ah I think your problem is with (.+) matching everything. Regex patterns are "greedy" and will try and match as much as they possibly can. That pattern matches the rest of the string, leaving nothing for the other groups.
所以在我看来,就像你想要的那样:
摄氏度温度的单位为 1 美元,华氏温度的单位为 2 美元,方法的单位为 3 美元。
([^$]+)
可能不正确,具体取决于您想要执行的操作,因为它将捕获到行尾的所有内容(例如空格,如果有的话)。您可以在最后使用(?: - ([^$]+?))?\s*$/
,我认为这可以解决这个问题。温度总是采用小数格式吗?它们可能只是“0c / 32f”吗?
编辑:刚刚看到您的更新。正如 Rob Agar 所建议的,贪婪的
.+
看起来确实是问题的一部分。你可以试试这个:即使方法不止一个单词,这也应该有效。不确定这是否可能,我正在对您的要求做出最好的猜测。
So it looks to me like you want:
The centigrade temp will be in $1, the Fahrenheit version will be in $2, and the method will be in $3.
([^$]+)
may not be correct dependiing on what you want to do, since it will capture everything up to the end of the line (like whitespace, if there is any). You could use(?: - ([^$]+?))?\s*$/
at the end instead, I think that would fix that.Are the temperatures always in a decimal format? Could they ever just be "0c / 32f"?
Edit: Just saw your update. It looks like the greedy
.+
is indeed part of the problem, as Rob Agar suggested. You can try this:That should work even if the method is more than one word. Not sure if that's a possibility, I'm making my best guess at your requirements.
基本上你需要一个“?”在方法的捕获组之后。这表明该组可能不存在,但整个模式应该仍然匹配。你现在的模式是什么样的?
Basically you need a '?' after the capturing group for the method. That indicates that the group may not be there, but the pattern as a whole should still match. What does your pattern look like at the moment?
我可能会在这里做出一些假设,但您可以尝试以下操作
由于所有子分组,您的匹配数组将包含比示例中更多的项目,但您要查找的项目应该在其中
I might be making some assumptions here but you could try the following
Due to all the sub-groupings, your matching array will contain more items than in your example but the one's you're after should be in there
$ptn = "/temp: (.)(\s-\s)?(.)/";
$ptn = "/temp: (.)(\s-\s)?(.)/";