【问题标题】:Moving the <a id> tag before the <a href> tag将 <a id> 标签移到 <a href> 标签之前
【发布时间】:2013-06-24 17:08:31
【问题描述】:

标签的位置需要在标签之前。有 500 个实例我需要排序。

例如:

<h2 class="chapter-title"><a href="contents.html#Endnotes"><a id="Anchor"/>Endnotes and References</a></h2>

我正在寻找

(<a [^>]*href\s*=\s*")([^"]+)("[^>]*>)(<a [^>]*id\s*=\s*")([^"]+)("[^>]*>) 

正确拾取两个标签,但正在努力寻找正确的替换来纠正位置。

救命!!???

【问题讨论】:

    标签: html tags grep position move


    【解决方案1】:

    这对我有用:

    $ sed 's#\(<a href[^>]*>\)\(<a id[^>]*>\)#\2\1#g' b
    <h2 class="chapter-title"><a id="Anchor"/><a href="contents.html#Endnotes">Endnotes and References</a></h2>
    

    我们查找文本&lt;a href[^&gt;] 并将其保存在第一个变量中。然后我们查找文本&lt;a id[^&gt;] 并保存在第二个变量中。最后我们交换它们。

    请注意,&lt;a id[^&gt;] 表示“从 &lt;a id 开始到 &gt; 字符的所有内容。

    【讨论】:

      【解决方案2】:

      我这应该可以。您只需要两个捕获组,一个用于&lt;a href...&gt;,另一个用于&lt;a id...&gt;

      sed 's#\(<a [^>]*href\s*=\s*"[^"]+"[^>]*>\)\(<a [^>]*id\s*=\s*"[^"]+"[^>]*>\)#\2\1#'
      

      【讨论】:

        【解决方案3】:

        你可以试试这个,我觉得应该可以的

        <a id="Anchor" href="contents.html#Endnotes">EndNotes and Refrences</a>
        

        【讨论】:

          猜你喜欢
          • 2021-03-03
          • 2021-12-26
          • 1970-01-01
          • 1970-01-01
          • 2012-05-15
          • 1970-01-01
          • 2019-10-17
          • 2019-05-15
          • 2010-10-27
          相关资源
          最近更新 更多