【问题标题】:Notepad++ deleting tags with specific text insideNotepad++ 删除带有特定文本的标签
【发布时间】:2016-05-30 14:37:23
【问题描述】:

我有一个包含产品的大型 XML 文件。我正在尝试删除所有缺货的产品。文件大小超过 20MB。

<product>
  <name>bla1</name>
  <price>50$</price>
  <stock>yes</stock>
  <description>bla</description>
</product>

<product>
  <name>bla2</name>
  <price>60$</price>
  <stock>no</stock>
  <description>bla</description>
</product>

...

是否可以使用 Notepad++ 的正则表达式删除它们,或者我应该使用 simpleXML(PHP) 或类似的东西?

我的基本 PHP 代码:

$url = 'input/products.xml';
    $xml = new SimpleXMLElement(file_get_contents($url));

    foreach ($xml->product->children() as $product) {

        //finding out of stock products and deleting them

    }
    $xml->asXml('output/products.xml');

【问题讨论】:

  • 每个product 都有一个stock 元素吗?
  • 该文件有超过 30k+ 行,但据我所知,是的。每个产品都有一个股票元素,里面有各种文本。 (有货,缺货,新进货)

标签: php regex xml notepad++


【解决方案1】:

转发

通过正则表达式进行模式匹配并不理想,如果您可以访问 PHP,那么我建议使用适当的 HTLM 解析工具。话虽如此,我提供了一个可以在 Notepad++ 中使用的解决方案

说明

&lt;product\s*(?:[^&gt;=]|='[^']*'|="[^"]*"|=[^'"][^\s&gt;]*)*?\s?\/?&gt;(?:(?!&lt;/product).)*&lt;stock\s*(?:[^&gt;=]|='[^']*'|="[^"]*"|=[^'"][^\s&gt;]*)*?\s?\/?&gt;no&lt;/stock&gt;(?:(?!&lt;/product).)*&lt;\/product&gt;

替换为: nothing

为了更好地查看图像,您可以右键单击它并选择在新窗口中查看。

此正则表达式将执行以下操作:

  • 找到整个产品部分
  • 需要子标签stock
  • 要求子标签stock 的值为no
  • 避免使 HTML 中的模式匹配变得困难的极端情况

从记事本++

在 Notepad++ 中,请注意您应该使用 notpad++ 6.1 或更高版本,因为旧版本中的正则表达式问题现已解决。

  1. ctrlh进入查找替换 模式

  2. 选择正则表达式选项

  3. 在“查找内容”字段中放置正则表达式

  4. 在“替换为”字段中输入``

  5. 点击全部替换

示例

现场演示

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

示例文本

<product>
  <name>bla1</name>
  <price>50$</price>
  <stock>yes</stock>
  <description>bla</description>
</product>

<product>
  <name>bla2</name>
  <price>60$</price>
  <stock>no</stock>
  <description>bla</description>
</product>

替换后

<product>
  <name>bla1</name>
  <price>50$</price>
  <stock>yes</stock>
  <description>bla</description>
</product>

说明

NODE                     EXPLANATION
----------------------------------------------------------------------
  <product                 '<product'
----------------------------------------------------------------------
  \s*                      whitespace (\n, \r, \t, \f, and " ") (0 or
                           more times (matching the most amount
                           possible))
----------------------------------------------------------------------
  (?:                      group, but do not capture (0 or more times
                           (matching the least amount possible)):
----------------------------------------------------------------------
    [^>=]                    any character except: '>', '='
----------------------------------------------------------------------
   |                        OR
----------------------------------------------------------------------
    ='                       '=\''
----------------------------------------------------------------------
    [^']*                    any character except: ''' (0 or more
                             times (matching the most amount
                             possible))
----------------------------------------------------------------------
    '                        '\''
----------------------------------------------------------------------
   |                        OR
----------------------------------------------------------------------
    ="                       '="'
----------------------------------------------------------------------
    [^"]*                    any character except: '"' (0 or more
                             times (matching the most amount
                             possible))
----------------------------------------------------------------------
    "                        '"'
----------------------------------------------------------------------
   |                        OR
----------------------------------------------------------------------
    =                        '='
----------------------------------------------------------------------
    [^'"]                    any character except: ''', '"'
----------------------------------------------------------------------
    [^\s>]*                  any character except: whitespace (\n,
                             \r, \t, \f, and " "), '>' (0 or more
                             times (matching the most amount
                             possible))
----------------------------------------------------------------------
  )*?                      end of grouping
----------------------------------------------------------------------
  \s?                      whitespace (\n, \r, \t, \f, and " ")
                           (optional (matching the most amount
                           possible))
----------------------------------------------------------------------
  \/?                      '/' (optional (matching the most amount
                           possible))
----------------------------------------------------------------------
  >                        '>\r\n'
----------------------------------------------------------------------
  (?:                      group, but do not capture (0 or more times
                           (matching the most amount possible)):
----------------------------------------------------------------------
    (?!                      look ahead to see if there is not:
----------------------------------------------------------------------
      </product                '</product'
----------------------------------------------------------------------
    )                        end of look-ahead
----------------------------------------------------------------------
    .                        any character except \n
----------------------------------------------------------------------
  )*                       end of grouping
----------------------------------------------------------------------
  <stock                   '<stock'
----------------------------------------------------------------------
  \s*                      whitespace (\n, \r, \t, \f, and " ") (0 or
                           more times (matching the most amount
                           possible))
----------------------------------------------------------------------
  (?:                      group, but do not capture (0 or more times
                           (matching the least amount possible)):
----------------------------------------------------------------------
    [^>=]                    any character except: '>', '='
----------------------------------------------------------------------
   |                        OR
----------------------------------------------------------------------
    ='                       '=\''
----------------------------------------------------------------------
    [^']*                    any character except: ''' (0 or more
                             times (matching the most amount
                             possible))
----------------------------------------------------------------------
    '                        '\''
----------------------------------------------------------------------
   |                        OR
----------------------------------------------------------------------
    ="                       '="'
----------------------------------------------------------------------
    [^"]*                    any character except: '"' (0 or more
                             times (matching the most amount
                             possible))
----------------------------------------------------------------------
    "                        '"'
----------------------------------------------------------------------
   |                        OR
----------------------------------------------------------------------
    =                        '='
----------------------------------------------------------------------
    [^'"]                    any character except: ''', '"'
----------------------------------------------------------------------
    [^\s>]*                  any character except: whitespace (\n,
                             \r, \t, \f, and " "), '>' (0 or more
                             times (matching the most amount
                             possible))
----------------------------------------------------------------------
  )*?                      end of grouping
----------------------------------------------------------------------
  \s?                      whitespace (\n, \r, \t, \f, and " ")
                           (optional (matching the most amount
                           possible))
----------------------------------------------------------------------
  \/?                      '/' (optional (matching the most amount
                           possible))
----------------------------------------------------------------------
  >no</stock>              '>no</stock>'
----------------------------------------------------------------------
  (?:                      group, but do not capture (0 or more times
                           (matching the most amount possible)):
----------------------------------------------------------------------
    (?!                      look ahead to see if there is not:
----------------------------------------------------------------------
      </product                '</product'
----------------------------------------------------------------------
    )                        end of look-ahead
----------------------------------------------------------------------
    .                        any character except \n
----------------------------------------------------------------------
  )*                       end of grouping
----------------------------------------------------------------------
  <                        '<'
----------------------------------------------------------------------
  \/                       '/'
----------------------------------------------------------------------
  product>                 'product>'
----------------------------------------------------------------------

【讨论】:

  • 感谢您的详细回答,就像一个魅力。我正在编写 PHP 脚本,它将处理来自供应商的多个 XML 文件并将它们集成到一个 CSV 文件中,但在那之前,我不得不手动编辑 XML 以适应我们的商店。
【解决方案2】:

我猜记事本++会更容易,即:

FIND : &lt;product&gt;\s+&lt;name&gt;.*?&lt;\/name&gt;\s+&lt;price&gt;.*?&lt;\/price&gt;\s+&lt;stock&gt;no&lt;\/stock&gt;\s+&lt;description&gt;.*?\/description&gt;\s+&lt;\/product&gt;
REPLACE : 什么都没有


演示

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


注意

请务必勾选底部的Regular Expression

【讨论】:

    【解决方案3】:

    您可以使用以下代码使用 PHP 来完成此操作

    <?php
        $url = 'input/products.xml';
        $xml = new SimpleXMLElement(file_get_contents($url));
        $i = count($xml) - 1; 
        for ($i; $i >= 0; --$i) {   
           $product = $xml->product[$i];
           if ($product->stock == "no") {
              unset($xml->product[$i]);
           }
        }
        $xml->asXml('output/products.xml');
        ?> 
    

    【讨论】:

    • 我已经用@RoYoMi 的回答解决了这个问题,但我肯定需要你对我正在处理的 PHP 脚本的回答,非常感谢。
    猜你喜欢
    • 2014-06-21
    • 1970-01-01
    • 1970-01-01
    • 2013-05-19
    • 2022-06-11
    • 1970-01-01
    • 2016-12-06
    • 2022-08-23
    • 1970-01-01
    相关资源
    最近更新 更多