[英]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(?!^)).*?\[([^\]]+)\]
(?:\\(:([^:]+):\\)|\\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";
}
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.