正则表达式匹配之间的任何内容
regex to match whatever between
拜托,我需要一些帮助来创建正确的正则表达式。
我想检测 Decode("
")
之间的任何内容给我这个输出 2%65%66%_WHATEVER_8%74%74
我尝试了很多,但没有任何东西能正确地为我提供我想要的准确输出。
我的代码:
$string = '
<td class="red"><script type="text/javascript">Decode("2%65%66%_WHATEVER_8%74%74")</script></td>
<td class="green"><script type="text/javascript">Decode("2%65%66%_WHATEVER_8%74%74")</script></td>
<td class="red"><script type="text/javascript">Decode("2%65%66%_WHATEVER_8%74%74")</script></td>
';
$pattern = '/Decode("([^*]+)")/i';
preg_match_all($pattern, $string, $matches);
print_r($matches[1]);
如评论中所述,您可以使用
Decode\("([^"]+)"\)
并取第一组,见a demo on regex101.com。
作为 PHP
演示:
<?php
$data = <<<DATA
<script type="text/javascript">Decode("2%65%66%_WHATEVER_8%74%74")</script>
DATA;
$regex = '~Decode\("([^"]+)"\)~';
if (preg_match_all($regex, $data, $matches)) {
print_r($matches[1]);
}
?>
根据您输入的字符串,您只需要以下模式:
/\("\K[^"]+/
此简短模式是 appropriate/accurate,因为您的目标双引号子字符串由前面的 (
.
唯一标识
preg_match_all()
将在全字符串匹配 ([0]
) 中提供所需的子字符串。与使用捕获组相比,这更快且更少膨胀输出数组。
\(
表示 "a literal opening parenthesis"。如果没有反斜杠,正则表达式会误解您的意思并认为 (
的意思是:"start capturing from this point".
\K
将重新开始全字符串匹配。
[^"]+
会贪婪地匹配一个或多个非双引号字符,并在遇到双引号之前停止。这是一个negated character class
。这些通常用于在保持准确性的同时提高效率。
代码:(演示:https://3v4l.org/UmaaC)
$string = '
<td class="red"><script type="text/javascript">Decode("2%65%66%_WHATEVER_8%74%74")</script></td>
<td class="green"><script type="text/javascript">Decode("2%65%66%_WHATEVER_8%74%74")</script></td>
<td class="red"><script type="text/javascript">Decode("2%65%66%_WHATEVER_8%74%74")</script></td>';
$pattern = '/\("\K[^"]+/';
preg_match_all($pattern, $string, $matches);
print_r($matches[0]);
输出:
Array
(
[0] => 2%65%66%_WHATEVER_8%74%74
[1] => 2%65%66%_WHATEVER_8%74%74
[2] => 2%65%66%_WHATEVER_8%74%74
)
拜托,我需要一些帮助来创建正确的正则表达式。
我想检测 Decode("
")
之间的任何内容给我这个输出 2%65%66%_WHATEVER_8%74%74
我尝试了很多,但没有任何东西能正确地为我提供我想要的准确输出。
我的代码:
$string = '
<td class="red"><script type="text/javascript">Decode("2%65%66%_WHATEVER_8%74%74")</script></td>
<td class="green"><script type="text/javascript">Decode("2%65%66%_WHATEVER_8%74%74")</script></td>
<td class="red"><script type="text/javascript">Decode("2%65%66%_WHATEVER_8%74%74")</script></td>
';
$pattern = '/Decode("([^*]+)")/i';
preg_match_all($pattern, $string, $matches);
print_r($matches[1]);
如评论中所述,您可以使用
Decode\("([^"]+)"\)
并取第一组,见a demo on regex101.com。
作为
PHP
演示:
<?php
$data = <<<DATA
<script type="text/javascript">Decode("2%65%66%_WHATEVER_8%74%74")</script>
DATA;
$regex = '~Decode\("([^"]+)"\)~';
if (preg_match_all($regex, $data, $matches)) {
print_r($matches[1]);
}
?>
根据您输入的字符串,您只需要以下模式:
/\("\K[^"]+/
此简短模式是 appropriate/accurate,因为您的目标双引号子字符串由前面的 (
.
preg_match_all()
将在全字符串匹配 ([0]
) 中提供所需的子字符串。与使用捕获组相比,这更快且更少膨胀输出数组。
\(
表示 "a literal opening parenthesis"。如果没有反斜杠,正则表达式会误解您的意思并认为 (
的意思是:"start capturing from this point".
\K
将重新开始全字符串匹配。
[^"]+
会贪婪地匹配一个或多个非双引号字符,并在遇到双引号之前停止。这是一个negated character class
。这些通常用于在保持准确性的同时提高效率。
代码:(演示:https://3v4l.org/UmaaC)
$string = '
<td class="red"><script type="text/javascript">Decode("2%65%66%_WHATEVER_8%74%74")</script></td>
<td class="green"><script type="text/javascript">Decode("2%65%66%_WHATEVER_8%74%74")</script></td>
<td class="red"><script type="text/javascript">Decode("2%65%66%_WHATEVER_8%74%74")</script></td>';
$pattern = '/\("\K[^"]+/';
preg_match_all($pattern, $string, $matches);
print_r($matches[0]);
输出:
Array
(
[0] => 2%65%66%_WHATEVER_8%74%74
[1] => 2%65%66%_WHATEVER_8%74%74
[2] => 2%65%66%_WHATEVER_8%74%74
)