简体   繁体   English

正则表达式始终匹配第一个组“ first”(在其他组之前)

[英]Regular expression to match always the first group 'first' (before) the other groups

Sorry for the stupid question, I'm pretty sure the regex needs lookahead / lookbehind and \\G anchor, but I'm not able to find a solution. 很抱歉这个愚蠢的问题,我很确定正则表达式需要提前/回头和\\G锚,但是我找不到解决方案。 Thanks in advance for the help. 先谢谢您的帮助。

I have this string: 我有这个字符串:

(:xyz:) word word [match1] word word [match2] word [match...] word ...

I need this result: 我需要这个结果:

Match 1:
Group 1 = xyz
Group 2 = match1
Match 2:
Group 1 = xyz
Group 2 = match2
Match 3:
Group 1 = xyz
Group 2 = match...

You can use lookbehind: 您可以使用lookbehind:

(?<=\(:(xyz):\).*)\[(.*?)\]

But you can do this only in a few languages, like the C# or VB.NET. 但是您只能使用几种语言来执行此操作,例如C#或VB.NET。 Or in Java, if you specify a max limit for the chars in the lookbehind (eg .{0,100} instead of .* ). 或在Java中,如果您为后面的字符指定了最大字符数(例如.{0,100}而不是.* )。

You can use this regex with \\G boundary matcher: 您可以将此正则表达式与\\G边界匹配器一起使用:

(?:\(:([^:]+):\)|\G(?!^)).*?\[([^\]]+)\]

RegEx Demo 正则演示

  • (?:\\(:([^:]+):\\)|\\G(?!^)) will match our first capturing group or end of previous match using \\G anchor. (?:\\(:([^:]+):\\)|\\G(?!^))将使用\\G锚匹配我们的第一个捕获组或上一个匹配的结尾。
  • .*?\\[([^\\]]+)\\] will match & capture text from 2nd group that is between [...] . .*?\\[([^\\]]+)\\]将匹配并捕获[...]之间的第二组文本。

Code: 码:

$re = '/(?:\(:([^:]+):\)|\G(?!^)).*?\[([^\]]+)\]/';
$str = '(:xyz:) word word [match1] word word [match2] word [match...] word ...';

preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);

// Print the entire match result

$match1 = "";
foreach($matches as $val) {
  //print_r($val);
  if (!empty($val[1])) {
    $match1 = $val[1];
  }
  echo "Match " . (++$i) . ":\n";
  echo "Group 1 = " . $match1 . "\n";
  echo "Group 2 = " . $val[2] . "\n";
}

Code Demo 代码演示

Lookahead is not limited to fixed string lengths, and in some cases (this included) we can abuse this fact. 前瞻不限于固定的字符串长度,在某些情况下(包括在内),我们可以滥用此事实。

function find($str) {
  $n = preg_match_all("/\](.*?)\[(?=.*\):(.*?):\()/", strrev($str), $matches);
  $result = Array();
  for ($i = 0; $i < $n; $i++) {
    $result[] = [strrev($matches[2][$i]), strrev($matches[1][$i])];
  }
  return $result;
}

$str = "(:xyz:) word word [match1] word word [match2] word [match...] word ...";
print_r(find($str));

# =>
Array
(
    [0] => Array
        (
            [0] => xyz
            [1] => match...
        )

    [1] => Array
        (
            [0] => xyz
            [1] => match2
        )

    [2] => Array
        (
            [0] => xyz
            [1] => match1
        )

)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 正则表达式在前%后和后%前匹配 - Regular expression to match after first % and before last % 正则表达式 - 如何在第一次匹配之前捕获? - Regular Expression - How do I capture before the first match? PHP正则表达式匹配第一次出现? - PHP regular expression match first occurrences? 如何在 PHP 中使用与整个字符串中的给定组匹配的正则表达式,而不是在第一次匹配时停止 - How can i use a regular expression in PHP that matches a given group in the whole string instead of stops at first match 正则表达式-从头到尾查找,但只有第一个匹配项 - Regular expression - find from start to end but only first match PHP正则表达式,以匹配空间和点之间的第一个单词 - php regular expression to match first word between space and point 正则表达式匹配一个文本块直到第一个双新行? - Regular expression to match a block of text up to the first double new line? 我想使用正则表达式匹配字符串的第一个世界 - I want to match the first world of the string using regular expression 正则表达式匹配不匹配字符串中第一个的“ hashtagged”单词 - Regular expression to match 'hashtagged' words not matching the first one in string 为什么这个正则表达式与php中的第一个结果不匹配? - Why does this regular expression not match the first result in php?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM