【问题标题】:Regex: find link in html that does not contain square bracket with text eg. '[Some_Random_text]' but can contain empty square bracket '[]' [duplicate]正则表达式:在 html 中查找不包含方括号和文本的链接,例如。 '[Some_Random_text]' 但可以包含空方括号'[]' [重复]
【发布时间】:2020-03-22 15:31:49
【问题描述】:

案例一:

示例 html:<a href="https://www.jessussaveme.com/saveme/c-from.html?random[for_god_sake_save_me]=anyonethere&no=fr&lang=fr">Test</a>

预期输出:

https://www.jessussaveme.com/saveme/c-from.html?random[for_god_sake_save_me]=anyonethere&no=fr&lang=fr

案例2:

示例 html:<a href="https://www.jessussaveme.com/saveme/c-from.html?random[]=anyonethere&no=fr&lang=fr">Test</a>

预期输出:没有。链接不应包含空方括号 []

案例3:

示例 html:<a href="https://www.jessussaveme.com/saveme/c-from.html?random=anyonethere&no=fr&lang=fr">Test</a>

预期输出:https://www.jessussaveme.com/saveme/c-from.html?random=anyonethere&no=fr&lang=fr

应选择哪些链接: 1.不包含任何方括号'[]'的链接 2. 包含非空方括号'[Some_random_text]'的链接

不应选择的链接: 包含空方括号 [] 的链接。

【问题讨论】:

标签: php regex regex-lookarounds regex-group


【解决方案1】:

您可以使用 jQuery 而不是正则表达式:

$("a").each(function(index) { // iterates all <a> elements
  console.log($(this).attr('href').includes('[]') ? '' : $(this).attr('href')); // check if contain "[]" or not.
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a href="https://www.jessussaveme.com/saveme/c-from.html?reges[for_god_sake_save_me]=anyonethere&no=fr&lang=fr">Test</a>

<a href="https://www.jessussaveme.com/saveme/c-from.html?reges[]=anyonethere&no=fr&lang=fr">Test</a>

<a href="https://www.jessussaveme.com/saveme/c-from.html?random=anyonethere&no=fr&lang=fr">Test</a>

除非你可以从a hrefyou shouldn't use regex to parse获取文本。


既然你已经说过你使用 PHP,你可以试试下面的 method 来提取 URL:

$html = '<a href="https://www.jessussaveme.com/saveme/c-from.html?reges[for_god_sake_save_me]=anyonethere&no=fr&lang=fr">Test</a>

    <a href="https://www.jessussaveme.com/saveme/c-from.html?reges[]=anyonethere&no=fr&lang=fr">Test</a>

    <a href="https://www.jessussaveme.com/saveme/c-from.html?random=anyonethere&no=fr&lang=fr">Test</a>';

$hrefs = array();

$dom = new DOMDocument();
$dom->loadHTML($html);

$tags = $dom->getElementsByTagName('a');
foreach ($tags as $tag) {
       $hrefs[] =  $tag->getAttribute('href');
}

并检查contain是否为空括号:

foreach($hrefs as $a) 
{
    if (strpos($a, '[]') == false) {
        echo 'true'; // doesn't contain empty bracket
    }
}

【讨论】:

  • 我在后端使用这个正则表达式和 PHP。
  • @user9974829 您可以在 PHP 中使用 jQuery,但您需要 AJAX 来在页面之间进行通信
  • 在 PHP 中使用 ajax 解析将导致与正则表达式相比显着减速。不会吧。 ?
  • @user9974829 没有。差不多。
【解决方案2】:

这个有效:

&lt;\S.*?=\"(.*reges\[\w+\].*)\"&gt;.*&gt;

你可以看到它在这里工作。它只匹配第 1 组中的第一个标签,而在第二种情况下 [ ] 为空时不返回任何内容。

https://regex101.com/r/cdvVnP/1

编辑:

对于第三种情况,它应该看起来像:

if( !str.contains("reges[")){
  //passed() -pick up tat link as string doesnt contain reges[] or reges [some text]
}else{
//match with <\S.*?=\"(.*reges\[\w+\].*)\">.*>
// if you find match then pickup that link from group 1
}

【讨论】:

  • @ Amey Shirke:感谢您的回复。我的错误我没有描述第三个案例。应传递不包含任何方括号的链接。请参考我刚刚更新的案例 3。请把我从正则表达式的血浴中拯救出来。
  • 为此,您可以在使用上述正则表达式匹配之前执行 !str.contains("reges[\w+]")
  • 是的,这可行,但 2 次比较将无效。 *reges: 只是一个随机词。
  • 更新了代码。String.contains 应该是您的关键字 + [ 例如:“reges[”
  • 正如其他人已经提到的那样,使用正则表达式进行 xml 解析可能不是最好的。就避免比较而言,我觉得这是你能得到的最接近的结果。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-02
  • 2019-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多