preg_match 正则表达式有点丢失
我是正则表达式的初学者,所以没过多久我就完全迷失了:]
我需要做什么:
我有一串值 'a:b,a2:b2,a3:b3, a4:b4',我需要通过给定的该对的第二个值 (b2) 搜索特定的值对(即:a2:b2),并获取该对的第一个值作为输出(a2)。
允许使用所有字符(除了分隔每对值的“,”),并且任何第二个值(b、b2、b3、b4)都是唯一的(不能在字符串中出现多次)
让我展示一个更好的示例因为前面可能不清楚:
这是一个字符串: 2 分钟:2,5 分钟:5,10 分钟:10,15 分钟:15,从不:0
搜索模式是: 5
我想,最好的方法是使用函数称为preg_match 具有子模式功能。
所以我尝试了以下操作:
$str = '2 minutes:2,5 minutes:5,10 minutes:10,15 minutes:15,20 minutes:20,30 minutes:30, never:0';
$re = '/(?P<name>\w+):5$/';
preg_match($re, $str, $matches);
echo $matches['name'];
想要的输出是“5 分钟”,但它不起作用。
我还想坚持使用 Perl-Compatible reg。表达式如上面的代码包含在 PHP 脚本中。
有人可以帮我吗?我现在有点绝望了,因为我已经把一天的大部分时间都花在这上面了……
谢谢你们所有人。
I'm a beginner in regular expression so it didn't take long for me to get totally lost :]
What I need to do:
I've got a string of values 'a:b,a2:b2,a3:b3,a4:b4' where I need to search for a specific pair of values (ie: a2:b2) by the second value of the pair given (b2) and get the first value of the pair as an output (a2).
All characters are allowed (except ',' which seperates each pair of values) and any of the second values (b,b2,b3,b4) is unique (cant be present more than once in the string)
Let me show a better example as the previous may not be clear:
This is a string: 2 minutes:2,5 minutes:5,10 minutes:10,15 minutes:15,never:0
Searched pattern is: 5
I thought, the best way was to use function called preg_match with subpattern feature.
So I tried the following:
$str = '2 minutes:2,5 minutes:5,10 minutes:10,15 minutes:15,20 minutes:20,30 minutes:30, never:0';
$re = '/(?P<name>\w+):5$/';
preg_match($re, $str, $matches);
echo $matches['name'];
Wanted output was '5 minutes' but it didn't work.
I would also like to stick with Perl-Compatible reg. expressions as the code above is included in a PHP script.
Can anyone help me out? I'm getting a little bit desperate now, as Ive spent on this most of the day by now ...
Thanks to all of you guys.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
输出:
Output:
除了表达式必须在 5 之后匹配
$
的问题(只有当 5 是最后一个元素时才有效)之外,您还想确保在5
之后什么都没有出现或者另一对来了;在该对的第一个元素之前是另一个元素或字符串的开头,并且您希望在该对的第一个元素中匹配多个\w
。Besides the problem of your expression having to match
$
after 5, which would only work if 5 were the last element, you also want to make sure that after5
either nothing comes or another pair comes; that before the first element of the pair comes either another element or the beginning of the string, and you want to match more than\w
in the first element of the pair.preg_match 调用肯定会更短,但我想我不会为正则表达式而烦恼,而只是使用字符串和数组操作。
A
preg_match
call will be shorter for certain, but I think I wouldn't bother with regular expressions, and instead just use string and array manipulations.几乎与@Michael 相同。它不搜索元素,而是构造字符串数组。您说值是唯一的,因此它们用作我的数组中的键:
然后访问元素非常简单:
Almost the same as
@Michael
's. It doesn't search for an element but constructs an array of the string. You say that values are unique so they are used as keys in my array:Then accessing an element is very simple: