RegEx 和多行 PO 文件

RegEx and multiline PO files

我正在尝试在 Notepad++ 中创建一个正则表达式,只是一个简单的搜索和替换。

行如下:

msgid ""

" CONSUMPTION_PLAN_ERR|The Finished Good's BOM has been changed since production was added.\n"

" To continue using this Job with the new BOM, please update lots and expiries.\n"

" Previous Production Records will not be updated.\n"

msgstr ""

" The Finished Good's BOM has been changed since production was added.\n"

" To continue using this Job with the new BOM, please update lots and expiries.\n"

" Previous Production Records will not be updated.\n"

我需要更改双引号,但仅限于 msgid 部分。这些双引号应改为小于 < 和大于 > 符号,因此结果如下:

msgid <>

< CONSUMPTION_PLAN_ERR|The Finished Good's BOM has been changed since production was added.\n>

< To continue using this Job with the new BOM, please update lots and expiries.\n>

< Previous Production Records will not be updated.\n>

msgstr ""

" The Finished Good's BOM has been changed since production was added.\n"

" To continue using this Job with the new BOM, please update lots and expiries.\n"

" Previous Production Records will not be updated.\n"

我需要一个匹配上述多行示例的正则表达式模式,无论需要更改多少行。

我用这个模式搜索:

msgid ""\r\n("(.+?)"\r\n){1,}

并用此模式替换:

msgid <>\r\n<>\r\n

这有点管用,但不是我想要的。它只复制最后一行,而不复制它上面的两行。我做错了什么,但不知道是什么。

建议?

您可以使用正则表达式,但它不简单且效率不高。更有效的解决方案是编写解析器,或使用更灵活的编程语言来使用至少两个正则表达式的组合:一个提取块,第二个替换引号。

单个正则表达式解决方案如下所示

查找内容(?s)(?:\G(?!^(?<=.))|^msgid)(?:(?!^msg(?:id|str))[^"])*?\K"((?:(?!^msg(?:id|str))[^"])*?)"
替换为<>

参见regex demo

详情

  • (?s) - 与 . 相同,当 ON
  • 时匹配换行符
  • (?:\G(?!^(?<=.))|^msgid) - 一行的开头 (^) 然后是 msgid,或者 (|) 上一个成功匹配的结尾 (\G(?!^(?<=.)))
  • (?:(?!^msg(?:id|str))[^"])*?
  • \K - 匹配重置运算符,匹配缓冲区被清除
  • " - 一个 "
  • ((?:(?!^msg(?:id|str))[^"])*?) - 捕获组 1:
    • (?:(?!^msg(?:id|str))[^"])*? - 除了 " 之外的任何 0 个或更多但尽可能少的字符的出现,不会启动 msgidmsgstr一行开头的字符序列
  • " - ".