[英]Regex - Match multiples of anything between [[[ and ]]]
I need to match anything between [[[ and ]]] using regex.我需要使用正则表达式匹配 [[[ 和 ]]] 之间的任何内容。 I then need to put all the values found between the brackets into an array.
然后我需要将括号之间找到的所有值放入一个数组中。
Example text:示例文本:
here is some 'test text [[[media-2 large right]]], [[[image-0 large left]]] the another token [[[image-1]]
From the above text I need to match the first two:从上面的文本我需要匹配前两个:
1, [[[media-2 large right]]]
2, [[[image-0 large left]]]
but not the last as it only has two [ at the end.但不是最后一个,因为它最后只有两个 [。
This checks for: 这将检查:
[[[
]
-or- ]
任何东西- 或 - ]
that are not followed by ]
]
没有跟着]
]]]
]]]
preg_match_all('/\[\[\[(?:(?:[^\]]*|]{1,2}(?!]))*)]]]/', $string, $matches);
print_r($matches[0]);
This regex has the benefit of matching ]
inside of the triple-bracket wrapper (eg [[[foo]bar]]]
. 此正则表达式匹配具有的益处
]
三重托架包装物内(例如[[[foo]bar]]]
Note: ]
does not need to be escaped, except inside character classes. 注意:
]
除了字符类之外,不需要进行转义。
A generic solution is this one: 一个通用的解决方案就是这个:
\[{3}(?=.*?\]{3}(?!\]))((?:(?!\]{3}(?!\])).)*)
It reads 它读
\[{3} # 3 opening square brackets
(?= # begin positive look-ahead ("followed by..."
.*?\]{3} # ...3 closing brackets, anywhere ahead (*see explanation below)
(?!\]) # negative look-ahead: no more ] after the 3rd one
) # end positive look-ahead
( # begin group 1
(?: # begin non-matching group (for atomic grouping)
(?! # begin negative look-ahead ("not followed by"):
\]{3} # ...3 closing square brackets
(?!\]) # negative look-ahead: no more ] after the 3rd one
) # end negative look-ahead
. # the next character is valid, match it
) # end non-matching group
) # end group 1 (will contain the wanted substring)
The positive look-ahead is a safeguard clause that allows the expression to fail fast when there is no "]]]"
in a long input string. 正向前瞻是一个安全保护条款,当长输入字符串中没有
"]]]"
时,它允许表达式快速失败。
Once it's established that a "]]]"
will follow at some point ahead in the string, the negative look-ahead makes sure that the expression correctly matches strings like this one: 一旦确定一个
"]]]"
将在字符串前面的某个点跟随,负面的前瞻确保表达式正确匹配像这样的字符串:
[[[foo [some text] bar]]]
^
+-------- most of the other solutions would stop at this point
This expression checks at every character whether three ]
follow or not, so in this example it would include the " bar"
. 这个表达式检查在每一个字符是否3
]
遵守或不遵守,所以在这个例子中,将包括" bar"
。
The "no more ] after the 3rd one"
part of the expression makes sure that the match does not end prematurely, so that in this case: "no more ] after the 3rd one"
表达式的一部分确保匹配不会过早结束,所以在这种情况下:
[[[foo [some text]]]]
the match would still be "foo [some text]"
. 比赛仍然是
"foo [some text]"
。
Without it the expression would stop too early ( "foo bar [some text"
). 没有它,表达式会过早停止(
"foo bar [some text"
)。
Side effect is that we do not need to actually match the "]]]"
, since the positive look-ahead made clear that they are there. 副作用是我们不需要实际匹配
"]]]"
,因为积极的前瞻清楚表明他们在那里。 We only need to match up to them, which the negative look-ahead does nicely. 我们只需要匹配它们,负面的前瞻很好。
Note that you need to run the expression in "dotall" mode if your input contains newline characters. 请注意,如果输入包含换行符,则需要以“dotall”模式运行表达式。
See also: http://rubular.com/r/QFo9jHEh9d 另见: http : //rubular.com/r/QFo9jHEh9d
更安全的解决方案:
\[{3}[^\]]+?\]{3}
I think this works: 我认为这有效:
\[\[\[(.*)\]\]\]
But it's probably the newb way to do it :) 但它可能是新的方式:)
If your string will always follow that format, subject
, size
, position
, you can use this: 如果您的字符串始终遵循该格式,
subject
, size
, position
,您可以使用:
$string = "here is some 'test text [[[media-2 right]]], [[[image-0]]] the another [[[image-1 left large]]] and token [[[image-1]]";
preg_match_all('/[\[]{3}(.*?)(.*?)?(.*?)?[\]]{3}/', $string, $matches);
print_r($matches);
PI Yirahz - Ratchet Squad Recordz © 2021
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.