首页 > 解决方案 > 如何在准确文本之前在 Notepad++ 中查找和替换?

问题描述

我有一个包含数千行(json 列)的文件,如下所示:

"128",
            "drugName_en": "Ampy 500mg 胶囊",
            "drugName_ar": "امبي 500 مجم كبسول",
            "scientificDrugId": "959",
            "proxyDrugId": "01",
            “prodCompDrugId”:“06”,
            "pack": "2x10Capsules",
            "unit": "باكت",
            “价格”:“0”,
            “折扣”:“00”,
            "categoryId": "15",
            “图像”:“ http://icons.iconarchive.com/icons/graphicloads/medical-health/256/medicine-box-icon.png ”
        },
        "129",
            "drugName_en": "Ampy C 10*10 C 500mg 胶囊",
            "drugName_ar": "امبي سي 500 مجم 10*10 كبسول",
            "scientificDrugId": "36",
            "proxyDrugId": "01",
            “prodCompDrugId”:“06”,
            "pack": "10x10Capsules",
            "unit": "باكت",
            “价格”:“2267”,
            “折扣”:“00”,
            "categoryId": "15",
            “图像”:“ http://icons.iconarchive.com/icons/graphicloads/medical-health/256/medicine-box-icon.png ”
        },

我需要将,之前的每个"drugName_en"替换为:{

这可以在记事本++中完成吗?

先感谢您。

标签: regexnotepad++

解决方案


这可以完成工作,保留换行符(无论它是什么):

  • Ctrl+H
  • 找什么:,(\R)(?=\s+"drugName_en")
  • 用。。。来代替::{$1
  • 检查 火柴盒
  • 检查 环绕
  • CHECK 正则表达式
  • Replace all

解释:

,                   # a comma
(\R)                # group 1, any kind of linebreak (i.e. \r, \n, \r\n)
(?=                 # positive lookahead, make sure we have after:
    \s+                 # 1 or more white spaces
    "drugName_en"       # literally
)                   # end lookahead

替代品:

/{                  # literally
$1                  # content of group 1, the linebreak

屏幕截图(之前):

在此处输入图像描述

屏幕截图(之后):

在此处输入图像描述


推荐阅读