Sublime中的正则表达式如何选择包括换行符在内的所有内容到终点

Posted

技术标签:

【中文标题】Sublime中的正则表达式如何选择包括换行符在内的所有内容到终点【英文标题】:REGEX in Sublime How to select everything including line breaks up to an end point 【发布时间】:2021-06-25 17:31:54 【问题描述】:

我有一个客户,我们正在将他们的网站从 Squarespace 转移到 Wordpress。从 Squarespace 导出帖子会产生大量不必要的代码,我正在尝试删除这些代码。

如果我在像 regex101 这样的在线测试器中运行这个正则表达式,它会突出显示我正在寻找的内容:

/\<hr \/><h3 style="white-space:pre-wrap;">YOU MAY ALSO LIKE:<\/h3>([\s\S]*?)<\/div>\]\]>/gms

更新:这是我在 Sublime 中使用表达式的方式:

<hr \/><h3 style="white-space:pre-wrap;">YOU MAY ALSO LIKE:<\/h3>([\s\S]*?)<\/div>\]\]>

但是,当我尝试在 Sublime 中运行它时,它不起作用。

有没有人知道我需要做什么才能让 Sublime 正常工作?

这是用于测试目的的内容:(如果您将此内容粘贴到 regex101.com 并使用上面的正则表达式,它将突出显示我要删除的内容。

    <?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:excerpt="http://wordpress.org/export/1.2/excerpt/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:wp="http://wordpress.org/export/1.2/">
  <channel>
    <title>CLIENT CLIENTLAST</title>
    <link>https://www.CLIENTDOMAIN.com</link>
    <pubDate>Tue, 05 Feb 2019 22:13:35 +0000</pubDate>
    <description />
    <language>en-US</language>
    <wp:wxr_version>1.2</wp:wxr_version>
    <wp:author>
      <wp:author_id>1055063081</wp:author_id>
      <wp:author_login>CLIENT</wp:author_login>
      <wp:author_email />
      <wp:author_display_name><![CDATA[CLIENT]]></wp:author_display_name>
      <wp:author_first_name><![CDATA[CLIENT]]></wp:author_first_name>
      <wp:author_last_name><![CDATA[CLIENTLAST]]></wp:author_last_name>
    </wp:author>
    <wp:category>
      <wp:cat_name><![CDATA[null - null]]></wp:cat_name>
      <wp:category_nicename>null-null</wp:category_nicename>
      <wp:category_parent />
    </wp:category>
    <item>
      <title>What if you looked at setbacks like this...</title>
      <link>/read/what-if-you-looked-at-setbacks-like-this</link>
      <content:encoded><![CDATA[
<p class="" style="white-space:pre-wrap;"><strong>P.S. </strong>To get where we’re headed… everyone’s got to come along. <a href="http://mail.CLIENTDOMAIN.com/public/webform/render_form/default/54059162ab2c79ad9748ecab5d91b62f/ftaf/j8q56xec9df941bkl0bkutruvho61/caywtervkjauiwcmovobcpcxwatgbdj?td=FkU7TlfUyInMpngpjQW4AgF0SHJPqflxpU8J2z0pRvVxtUn0xp-jMIiLxzCTMpnjAe3su9g3E9NLEnQnDBEgeuYhLAQAMvn8GQHtyNL1VcUn713nINRMyd1VD5Obt8qRKx2-YCrAlIRGO3RFH74C5eHlVwDjFWibgB55olOWUU835F6A9qSjewvA&amp;tid=2443574726270008515295681973702067450845120587963545907028920327188514310" target="_blank"><strong>Forward this</strong></a> to a friend who you know is up for a year of inclusive aspiration.<br><br><strong>Tweet it out: </strong><br><br><a href="https://twitter.com/intent/tweet?text=What%20if%20every%20obstacle%20in%20your%20path%20was%20a%20gift%20of%20Loving%20Kindness%20from%20your%20Higher%20Self?%20Suddenly%20setbacks%20become%20love%20notes%20inviting%20us%20into%20deeper%20exploration%20of%20our%20True%20Nature.%20Class%20w/%20%40CLIENTDOMAIN%20starts%204/1.%20Door%E2%80%99s%20open%3A%20CLIENTDOMAIN.com/heartcentered%20%23heartcentered" target="_blank"><strong>What if every obstacle in your path was a gift of Loving Kindness from your Higher Self? Suddenly setbacks become love notes, inviting us into deeper exploration of our True Nature.</strong></a><br><br><a href="https://twitter.com/intent/tweet?text=Welcome%20and%20befriend%20it%20all.%20This%20is%20Loving%20Kindness%20training.%20This%20is%20how%20we%20create%20a%20New%20Earth.%20Class%20w/%20%40CLIENTDOMAIN%20starts%204/1.%20Door%E2%80%99s%20open%3A%20CLIENTDOMAIN.com/heartcentered%20%23heartcentered%20%23lovingkindness%20%23freewill" target="_blank"><strong>Welcome and befriend it all. This is Loving Kindness training. This is how we create a New Earth.</strong></a><br><br><strong>Click </strong><a href="https://pinterest.com/pin/create/button/?url=https%3A//CLIENTDOMAIN.mykajabi.com/heart-centered-2021&amp;media=https%3A//mcusercontent.com/ed442308ef864ebe9e3b10cf4/images/590e1fc2-7457-4243-bcc3-0a853dfcb92b.png&amp;description=Welcome%20and%20befriend%20it%20all.%20This%20is%20Loving%20Kindness%20training.%20This%20is%20how%20we%20create%20a%20New%20Earth.%20Class%20w/%20%40CLIENTDOMAIN%20starts%204/1.%20Door%E2%80%99s%20open%3A%20CLIENTDOMAIN.com/heartcentered%20%23heartcentered%20%23lovingkindness%20%23freewill" target="_blank"><strong>here</strong></a><strong> to Pin or right click to download + share on Instagram.</strong></p><hr /><h3 style="white-space:pre-wrap;">YOU MAY ALSO LIKE:</h3>



<div class="
  summary-block-wrapper

  summary-block-collection-type-blog

  summary-block-setting-text-size-medium
  summary-block-setting-text-align-left

  summary-block-setting-design-list
  summary-block-setting-design-list-thumbnail-left

  summary-block-setting-metadata-position-below-content
  summary-block-setting-primary-metadata-none
  summary-block-setting-secondary-metadata-none

  summary-block-setting-show-thumbnail
  summary-block-setting-show-title
  summary-block-setting-show-price
  summary-block-setting-show-excerpt
  summary-block-setting-show-read-more-link

  

  sqs-gallery-design-list

  
  "

  
>

  <div class="summary-item-list-container sqs-gallery-container">

    <header class="summary-block-header">

      <!-- Collection Title -->
      <div class="summary-heading" data-animation-role="content">
        
          <span class="summary-header-text">Featured</span>
        
      </div>

      <!-- Carousel Nav -->
      <div class="summary-carousel-pager sqs-gallery-controls" data-animation-role="content">
        <span
          class="summary-carousel-pager-prev previous"
          tabindex="0"
          role="button"
          aria-label="Previous"
        ></span>
        <span
          class="summary-carousel-pager-next next"
          tabindex="0"
          role="button"
          aria-label="Next"
        ></span>
      </div>

    </header>

    <div class="summary-item-list sqs-gallery">

      

        <div class="
          summary-item
          summary-item-record-type-text
          sqs-gallery-design-list-slide
          
           summary-item-has-thumbnail
           summary-item-has-excerpt
          summary-item-has-cats
          summary-item-has-tags
          summary-item-has-author
          
          "
          >

          
            <!-- Thumbnail -->
            

  
  <div class="summary-thumbnail-outer-container">
    <a
      
        
          href="/read/betrayal-denial-and-the-bloody-beauty-of-it-all"
        
      
      class="
        summary-thumbnail-container
        sqs-gallery-image-container
        
      "
      data-title="Been burned? Betrayal, denial, and the bloody beauty of it all."
      data-description=""
    >
      <div class="summary-thumbnail img-wrapper" data-animation-role="image">
  

    

        <!-- Main Image -->
        <img src="https://images.SOURCE-cdn.com/content/v1/5c5a0a8f9b7d1526e92feb59/1605112015118-3T1632NBIJK9KI0FD3LJ/ke17ZwdGBToddI8pDm48kF5UcQgsrEa5M_kXB3FEJvp7gQa3H78H3Y0txjaiv_0fDoOvxcdMmMKkDsyUqMSsMWxHk725yiiHCCLfrh8O1z5QHyNOqBUUEtDDsRWrJLTmYOVD9IE-axc7RUTDo4v7t9wzK2NOQ9LLKO3ltw4ueDjZj2DuUPWsPw7nxIZ6vyu1/WisdomPost-11.16.2020-Betrayal.Greatest+Hits_SQ.Feature.CLIENT+CLIENTLAST.png" data-image="https://images.SOURCE-cdn.com/content/v1/5c5a0a8f9b7d1526e92feb59/1605112015118-3T1632NBIJK9KI0FD3LJ/ke17ZwdGBToddI8pDm48kF5UcQgsrEa5M_kXB3FEJvp7gQa3H78H3Y0txjaiv_0fDoOvxcdMmMKkDsyUqMSsMWxHk725yiiHCCLfrh8O1z5QHyNOqBUUEtDDsRWrJLTmYOVD9IE-axc7RUTDo4v7t9wzK2NOQ9LLKO3ltw4ueDjZj2DuUPWsPw7nxIZ6vyu1/WisdomPost-11.16.2020-Betrayal.Greatest+Hits_SQ.Feature.CLIENT+CLIENTLAST.png" data-image-dimensions="1081x1080" data-image-focal-point="0.5,0.5"   data-load="false" class="summary-thumbnail-image"/>

    

    

        

        

      </div>
    </a>

    <!-- Products: Quick View -->
    

  </div>
  



          

          <div class="summary-content sqs-gallery-meta-container" data-animation-role="content">

            <!-- Metadata (Above Title) -->
            <div class="summary-metadata-container summary-metadata-container--above-title">
              
              <div class="summary-metadata summary-metadata--primary">

    












</div>
              
              <div class="summary-metadata summary-metadata--secondary">

    












</div>
            </div>

            
              <!-- Title -->
              <div class="summary-title">
                <a
                
                  
                    href="/read/betrayal-denial-and-the-bloody-beauty-of-it-all"
                  
                
                class="summary-title-link">Been burned? Betrayal, denial, and the bloody beauty of it all.</a></div>
            

            <!-- Metadata (Below Title) -->
            <div class="summary-metadata-container summary-metadata-container--below-title">
            
              <div class="summary-metadata summary-metadata--primary">

    












</div>
              
              <div class="summary-metadata summary-metadata--secondary">

    












</div>
            </div>

            
              
            

            
              
              <!-- Excerpt -->
                <div class="summary-excerpt">
                  <p style="white-space:pre-wrap;">I’ve been betrayed — severely. Thank God. Betrayal is such a defining experience — it lays your heart bare, and that’s a bloody, good thing. Bloody good.</p><p style="white-space:pre-wrap;">Being betrayed by another person is often (not always,) a reflection of how you were betraying yourself. It’s a lie looking back at you.</p>
                </div>
              

              

              
                <!-- Read More Link -->
                <a href="/read/betrayal-denial-and-the-bloody-beauty-of-it-all" class="summary-read-more-link">Read More &#8594;</a>
              
            

            <!-- Metadata (Below Content) -->
            <div class="summary-metadata-container summary-metadata-container--below-content">
              
              <div class="summary-metadata summary-metadata--primary">

    












</div>
              
              <div class="summary-metadata summary-metadata--secondary">

    












</div>
            </div>

          </div> <!-- End .summary-content -->

        </div> <!-- End .summary-item -->

      

        <div class="
          summary-item
          summary-item-record-type-text
          sqs-gallery-design-list-slide
          
           summary-item-has-thumbnail
           summary-item-has-excerpt
          summary-item-has-cats
          summary-item-has-tags
          summary-item-has-author
          
          "
          >

          
            <!-- Thumbnail -->
            

  
  <div class="summary-thumbnail-outer-container">
    <a
      
        
          href="/read/defining-spiritual-bypassing"
        
      
      class="
        summary-thumbnail-container
        sqs-gallery-image-container
        
      "
      data-title="Defining “Spiritual bypassing.” Overused and misused but still… very useful."
      data-description=""
    >
      <div class="summary-thumbnail img-wrapper" data-animation-role="image">
  

    

        <!-- Main Image -->
        <img src="https://images.SOURCE-cdn.com/content/v1/5c5a0a8f9b7d1526e92feb59/1601571151722-TZFB8N3D3OGUS435OODH/ke17ZwdGBToddI8pDm48kF5UcQgsrEa5M_kXB3FEJvp7gQa3H78H3Y0txjaiv_0fDoOvxcdMmMKkDsyUqMSsMWxHk725yiiHCCLfrh8O1z5QHyNOqBUUEtDDsRWrJLTmYOVD9IE-axc7RUTDo4v7t9wzK2NOQ9LLKO3ltw4ueDjZj2DuUPWsPw7nxIZ6vyu1/WisdomPost-10.5.2020-PleasantDistractions_SQ.Feature.CLIENT+CLIENTLAST.png" data-image="https://images.SOURCE-cdn.com/content/v1/5c5a0a8f9b7d1526e92feb59/1601571151722-TZFB8N3D3OGUS435OODH/ke17ZwdGBToddI8pDm48kF5UcQgsrEa5M_kXB3FEJvp7gQa3H78H3Y0txjaiv_0fDoOvxcdMmMKkDsyUqMSsMWxHk725yiiHCCLfrh8O1z5QHyNOqBUUEtDDsRWrJLTmYOVD9IE-axc7RUTDo4v7t9wzK2NOQ9LLKO3ltw4ueDjZj2DuUPWsPw7nxIZ6vyu1/WisdomPost-10.5.2020-PleasantDistractions_SQ.Feature.CLIENT+CLIENTLAST.png" data-image-dimensions="1081x1080" data-image-focal-point="0.5,0.5"   data-load="false" class="summary-thumbnail-image"/>

    

    

        

        

      </div>
    </a>

    <!-- Products: Quick View -->
    

  </div>
  



          

          <div class="summary-content sqs-gallery-meta-container" data-animation-role="content">

            <!-- Metadata (Above Title) -->
            <div class="summary-metadata-container summary-metadata-container--above-title">
              
              <div class="summary-metadata summary-metadata--primary">

    












</div>
              
              <div class="summary-metadata summary-metadata--secondary">

    












</div>
            </div>

            
              <!-- Title -->
              <div class="summary-title">
                <a
                
                  
                    href="/read/defining-spiritual-bypassing"
                  
                
                class="summary-title-link">Defining “Spiritual bypassing.” Overused and misused but still… very useful.</a></div>
            

            <!-- Metadata (Below Title) -->
            <div class="summary-metadata-container summary-metadata-container--below-title">
            
              <div class="summary-metadata summary-metadata--primary">

    












</div>
              
              <div class="summary-metadata summary-metadata--secondary">

    












</div>
            </div>

            
              
            

            
              
              <!-- Excerpt -->
                <div class="summary-excerpt">
                  <p class="">Like too much of anything, spiritual bypassing can numb us out from our Truth—which is where the healing answers wait to be found.</p>
                </div>
              

              

              
                <!-- Read More Link -->
                <a href="/read/defining-spiritual-bypassing" class="summary-read-more-link">Read More &#8594;</a>
              
            

            <!-- Metadata (Below Content) -->
            <div class="summary-metadata-container summary-metadata-container--below-content">
              
              <div class="summary-metadata summary-metadata--primary">

    












</div>
              
              <div class="summary-metadata summary-metadata--secondary">

    












</div>
            </div>

          </div> <!-- End .summary-content -->

        </div> <!-- End .summary-item -->

      

        <div class="
          summary-item
          summary-item-record-type-text
          sqs-gallery-design-list-slide
          
           summary-item-has-thumbnail
           summary-item-has-excerpt
          summary-item-has-cats
          summary-item-has-tags
          summary-item-has-author
          
          "
          >

          
            <!-- Thumbnail -->
            

  
  <div class="summary-thumbnail-outer-container">
    <a
      
        
          href="/read/cognitive-dissonance-and-luminosity"
        
      
      class="
        summary-thumbnail-container
        sqs-gallery-image-container
        
      "
      data-title="Cognitive dissonance and our luminosity"
      data-description=""
    >
      <div class="summary-thumbnail img-wrapper" data-animation-role="image">
  

    

        <!-- Main Image -->
        <img src="https://images.SOURCE-cdn.com/content/v1/5c5a0a8f9b7d1526e92feb59/1600250171098-SRUG7PPL0FA1TFPW6EWQ/ke17ZwdGBToddI8pDm48kF5UcQgsrEa5M_kXB3FEJvp7gQa3H78H3Y0txjaiv_0fDoOvxcdMmMKkDsyUqMSsMWxHk725yiiHCCLfrh8O1z5QHyNOqBUUEtDDsRWrJLTmYOVD9IE-axc7RUTDo4v7t9wzK2NOQ9LLKO3ltw4ueDjZj2DuUPWsPw7nxIZ6vyu1/WisdomPost-9.21.2020-Cognitive-Dissonance_SQ.Feature.CLIENT+CLIENTLAST.png" data-image="https://images.SOURCE-cdn.com/content/v1/5c5a0a8f9b7d1526e92feb59/1600250171098-SRUG7PPL0FA1TFPW6EWQ/ke17ZwdGBToddI8pDm48kF5UcQgsrEa5M_kXB3FEJvp7gQa3H78H3Y0txjaiv_0fDoOvxcdMmMKkDsyUqMSsMWxHk725yiiHCCLfrh8O1z5QHyNOqBUUEtDDsRWrJLTmYOVD9IE-axc7RUTDo4v7t9wzK2NOQ9LLKO3ltw4ueDjZj2DuUPWsPw7nxIZ6vyu1/WisdomPost-9.21.2020-Cognitive-Dissonance_SQ.Feature.CLIENT+CLIENTLAST.png" data-image-dimensions="1081x1080" data-image-focal-point="0.5,0.5"   data-load="false" class="summary-thumbnail-image"/>

    

    

        

        

      </div>
    </a>

    <!-- Products: Quick View -->
    

  </div>
  



          

          <div class="summary-content sqs-gallery-meta-container" data-animation-role="content">

            <!-- Metadata (Above Title) -->
            <div class="summary-metadata-container summary-metadata-container--above-title">
              
              <div class="summary-metadata summary-metadata--primary">

    












</div>
              
              <div class="summary-metadata summary-metadata--secondary">

    












</div>
            </div>

            
              <!-- Title -->
              <div class="summary-title">
                <a
                
                  
                    href="/read/cognitive-dissonance-and-luminosity"
                  
                
                class="summary-title-link">Cognitive dissonance and our luminosity</a></div>
            

            <!-- Metadata (Below Title) -->
            <div class="summary-metadata-container summary-metadata-container--below-title">
            
              <div class="summary-metadata summary-metadata--primary">

    












</div>
              
              <div class="summary-metadata summary-metadata--secondary">

    












</div>
            </div>

            
              
            

            
              
              <!-- Excerpt -->
                <div class="summary-excerpt">
                  <p class=""><a href="https://twitter.com/intent/tweet?text=If%20you%E2%80%99re%20paying%20attention%20to%20your%20choices,%20you%E2%80%99ll%20cognitive%20dissonance%20pang%20once%20in%20a%20while.%20%E2%80%94%20%40CLIENTDOMAIN%20https%3A//BLAH/3bAPDNF%20%23cognitivedissonance%20%23luminous" target="_blank"><strong>If you’re paying attention to your choices, you’ll cognitive dissonance pang once in a while.</strong></a><strong> And it can be a smidge shamey. But when you apply compassion to that sensation it shows itself as your conscience. And it’s a signal to </strong><a href="https://www.CLIENTDOMAIN.com/read/the-in-between-place-youre-in-its-real"><strong>get into alignment with your higher nature</strong></a><strong>. Either examine and change your perspective, or shift your actions.</strong></p>
                </div>
              

              

              
                <!-- Read More Link -->
                <a href="/read/cognitive-dissonance-and-luminosity" class="summary-read-more-link">Read More &#8594;</a>
              
            

            <!-- Metadata (Below Content) -->
            <div class="summary-metadata-container summary-metadata-container--below-content">
              
              <div class="summary-metadata summary-metadata--primary">

    












</div>
              
              <div class="summary-metadata summary-metadata--secondary">

    












</div>
            </div>

          </div> <!-- End .summary-content -->

        </div> <!-- End .summary-item -->

      

    </div> <!-- End .summary-item-list -->

  </div> <!-- End .summary-item-list-container -->

</div>]]></content:encoded>
      <excerpt:encoded><![CDATA[<p class="">What if <em>every obstacle</em> in your path was a<strong> </strong><a href="https://CLIENTDOMAIN.mykajabi.com/heart-centered-2021" target="_blank"><strong>gift of Loving Kindness</strong></a> from your Higher Self? An opportunity to transform a wound into Divine Power. Fractured awareness into wisdom. Contraction into openness… <br><br>Suddenly setbacks become love notes, inviting us into <a href="https://CLIENTDOMAIN.mykajabi.com/heart-centered-2021" target="_blank">deeper exploration of our True Nature</a>.</p>]]></excerpt:encoded>
      <wp:post_name>what-if-you-looked-at-setbacks-like-this</wp:post_name>
      <wp:post_type>post</wp:post_type>
      <wp:post_id>0</wp:post_id>
      <wp:status>pending</wp:status>
      <pubDate>Tue, 30 Mar 2021 12:00:00 +0000</pubDate>
      <wp:post_date>2021-03-30 12:00:00</wp:post_date>
      <wp:post_date_gmt>2021-03-30 12:00:00</wp:post_date_gmt>
      <category domain="post_tag" nicename="the-latest"><![CDATA[the latest]]></category>
      <category domain="post_tag" nicename="heart-centered"><![CDATA[heart centered]]></category>
      <category domain="post_tag" nicename="membership"><![CDATA[membership]]></category>
      <category domain="post_tag" nicename="practices"><![CDATA[practices]]></category>
      <category domain="post_tag" nicename="meditation"><![CDATA[meditation]]></category>
      <category domain="post_tag" nicename="journaling"><![CDATA[journaling]]></category>
      <category domain="category" nicename="heart-centered"><![CDATA[Heart Centered]]></category>
      <category domain="category" nicename="the-latest"><![CDATA[the latest]]></category>
      <dc:creator>CLIENT</dc:creator>
      <wp:comment_status>closed</wp:comment_status>
      <wp:postmeta>
        <wp:meta_key>_thumbnail_id</wp:meta_key>
        <wp:meta_value><![CDATA[1]]></wp:meta_value>
      </wp:postmeta>
    </item>
  </channel>
</rss>

【问题讨论】:

我认为你必须删除领先的/ 和末尾的/gms,如\&lt;hr \/&gt;&lt;h3 style="white-space:pre-wrap;"&gt;YOU MAY ALSO LIKE:&lt;\/h3&gt;([\s\S]*?)&lt;\/div&gt;\]\]&gt; @Thefourthbird 是的,我已经删除了这些,但它仍然不起作用。你有崇高吗?如果是这样,请复制并粘贴内容,然后在我发布的表达式中查找并粘贴正则表达式,减去前面和结尾的转义符。 @Thefourthbird 我一定错过了逃跑,因为你的方法奏效了!将此作为答案,我将排除它。谢谢 【参考方案1】:

根据Sublime Text Unofficial Documentation,Sublime 使用 Boost 库,而模式 \&lt; 开头的这部分是 Word boundary,因此您在 &lt;hr /&gt; 中缺少前导 &lt;,因为有一个词&lt;h 之间的边界

此外,在您尝试的模式中,可能会复制前导 / 和尾随 /gms/ 表示模式分隔符,gms 表示标志。

这样的格式可以与 javascript 一起使用,但在 Sublime 中,它会按字面意思匹配这些字符。

在您最终使用的模式中,您不必转义],也不必转义/

模式可能如下所示:

<hr /><h3 style="white-space:pre-wrap;">YOU MAY ALSO LIKE:</h3>([\s\S]*?)</div>]]>

【讨论】:

以上是关于Sublime中的正则表达式如何选择包括换行符在内的所有内容到终点的主要内容,如果未能解决你的问题,请参考以下文章

Python: 正则表达式匹配多行,实现多行匹配模式

正则表达式

正则表达式

python多行正则匹配

Python re模块 正则表达式之compile函数

正则表达式