preg_match 正则表达式有点丢失

发布于 2024-12-05 00:01:19 字数 750 浏览 0 评论 0原文

我是正则表达式的初学者,所以没过多久我就完全迷失了:]

我需要做什么:

我有一串值 '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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

﹏半生如梦愿梦如真 2024-12-12 00:01:19
$str = '2 minutes:2,51 seconds:51,5 minutes:5,10 minutes:10,15 minutes:51,never:0';
$search = 5;

preg_match("~([^,\:]+?)\:".preg_quote($search)."(?:,|$)~", $str, $m);

echo '<pre>'; print_r($m); echo '</pre>';

输出:

Array
(
    [0] => 5 minutes:5
    [1] => 5 minutes
)
$str = '2 minutes:2,51 seconds:51,5 minutes:5,10 minutes:10,15 minutes:51,never:0';
$search = 5;

preg_match("~([^,\:]+?)\:".preg_quote($search)."(?:,|$)~", $str, $m);

echo '<pre>'; print_r($m); echo '</pre>';

Output:

Array
(
    [0] => 5 minutes:5
    [1] => 5 minutes
)
雨的味道风的声音 2024-12-12 00:01:19
$re = '/(?:^|,)(?P<name>[^:]*):5(?:,|$)/';

除了表达式必须在 5 之后匹配 $ 的问题(只有当 5 是最后一个元素时才有效)之外,您还想确保在 5 之后什么都没有出现或者另一对来了;在该对的第一个元素之前是另一个元素或字符串的开头,并且您希望在该对的第一个元素中匹配多个 \w

$re = '/(?:^|,)(?P<name>[^:]*):5(?:,|$)/';

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 after 5 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.

恋你朝朝暮暮 2024-12-12 00:01:19

preg_match 调用肯定会更短,但我想我不会为正则表达式而烦恼,而只是使用字符串和数组操作。

$pairstring = '2 minutes:2,5 minutes:5,10 minutes:10,15 minutes:15,20 minutes:20,30 minutes:30, never:0';

function match_pair($searchval, $pairstring) {
  $pairs = explode(",", $str);
  foreach ($pairs as $pair) {
    $each = explode(":", $pair);
    if ($each[1] == $searchval) {
      echo $each[0];
    }
  }
}

// Call as:
match_pair(5, $pairstring);

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.

$pairstring = '2 minutes:2,5 minutes:5,10 minutes:10,15 minutes:15,20 minutes:20,30 minutes:30, never:0';

function match_pair($searchval, $pairstring) {
  $pairs = explode(",", $str);
  foreach ($pairs as $pair) {
    $each = explode(":", $pair);
    if ($each[1] == $searchval) {
      echo $each[0];
    }
  }
}

// Call as:
match_pair(5, $pairstring);
靖瑶 2024-12-12 00:01:19

几乎与@Michael 相同。它不搜索元素,而是构造字符串数组。您说值是唯一的,因此它们用作我的数组中的键:

$str = '2 minutes:2,5 minutes:5,10 minutes:10,15 minutes:15,20 minutes:20,30 minutes:30, never:0';
$a = array();
foreach(explode(',', $str) as $elem){
    list($key, $val) = explode(':', $elem);
    $a[$val] = $key;
}

然后访问元素非常简单:

echo $a[5];

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:

$str = '2 minutes:2,5 minutes:5,10 minutes:10,15 minutes:15,20 minutes:20,30 minutes:30, never:0';
$a = array();
foreach(explode(',', $str) as $elem){
    list($key, $val) = explode(':', $elem);
    $a[$val] = $key;
}

Then accessing an element is very simple:

echo $a[5];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文