【问题标题】:Replacing segments from an EDI file using regex, if in comments?如果在注释中,使用正则表达式替换 EDI 文件中的段?
【发布时间】:2017-03-04 06:41:09
【问题描述】:

我正在尝试使用 powershell 从文件中删除敏感数据并将其替换为虚拟数据,但前提是它位于注释块中(在一行 -- 之后,或在 /**/ 之间,可以跨行)

所以一个虚拟的例子是

So |AAthis|ABpart|is fine, as is this -- but remove ('|AAthis|ABthisisfine|ACremovethis|ADandleavetherest', more line here,3)

我希望替换为

So |AAthis|ABpart|is fine, as is this-- but remove ('|AATHAT|ABthisisfine|ACANDREMOVETHAT|ADandleavetherest', more line here,3)

在 powershell 正则表达式中,我已经接近了:

$var= "So |AAthis|ABpart|is fine, as is this -- but remove ('|AAthis|ABthisisfine|ACremovethis|ADandleavetherest', more line here,3)"

$var -replace '(.*--.*\|AAthis).*(\|.*)',  ('$1' + 'THAT' + '$2')

,但是(a)我还没有处理多行块,它贪婪并删除所有中间段有问题

So |AAthis|ABpart|is fine, as is this -- but remove ('|AAthisTHAT|ADandleavetherest', more line here,3)

【问题讨论】:

  • 解析出 cmets 很容易,但如果文件有任何被解释为引号的结构,它们也必须被解析。有报价吗?另外,我没看错,注释语法是-- 到行尾,/* .. */ 可以跨行?
  • 不确定您对“构造为引号”的含义。上面的例子是我最期望的样子,它在引号和刻度内。注释语法:正确。 (这是 SQL cmets,如果有帮助的话)
  • 这个文件是用来解析关键字或语法的吗?如果引号是可解析的,您必须能够区分它们。示例:If this 'is a quote' Then this 'here -- is not a comment'
  • 你发现了什么?
  • EDI 字符串中不应包含引号,但显然它可以嵌入引号中。(在示例中,它是嵌入的)

标签: sql regex powershell


【解决方案1】:

您必须对每个单独的项目进行全局查找/替换。
IE。如果您有超过 1 个项目,则必须使用单独的正则表达式。

但是,您可以在
两种类型的 cmets --/* .. */ 扫描 同时。

这两种类型只有一个交替。
第 1,2 组覆盖 /* */
类型之前/之后 第 3,4 组覆盖 -- 类型之前/之后。

一次只能匹配一组组,因此它只是一个简单的串联
组的更换。

注意
您必须重新运行每个正则表达式,直到找不到要替换的内容。
这是因为,它一次只替换一个实例(尽管是全局的)。
在同一条评论中,您可能有更多的实例彼此相邻。
由于匹配会消耗整个评论,因此必须从
一直到找不到为止。

对于您的示例,它是全局的:

查找(/\*(?:(?!\*/)[\S\s])*?)\|AAthis\|((?:(?!\*/)[\S\s])*?\*/)|(--.*?)\|AAthis\|(.*)
替换$1$3|replaced|$2$4

解释

    (                             # (1 start), /* .. */ comment
         /\*     
         (?:                           # Before
              (?! \*/ )                     # Not closure
              [\S\s]                        # Dot-all
         )*?
    )                             # (1 end)
    \|AAthis\|                    # What to find in between
    (                             # (2 start)
         (?:                           # After

              (?! \*/ )                     # Not closure
              [\S\s]                        # Dot-all
         )*?
         \*/                           # Closure
    )                             # (2 end)
 |                            # or,
    (                             # (3 start), -- line comment
         --
         .*? 
    )                             # (3 end)
    \|AAthis\|                    # What to find after -- 
    (                             # (4 start)
         .* 
    )                             # (4 end)

示例输入:

 "So |AAthis|ABpart|is fine, as is this -- but remove ('|AAthis|ABthisisfine|ACremovethis|ADandleavetherest', more line here,3)"
 "And |AAthis|ABpart|is fine, as is this /* but remove
 ('|AAthis|ABthisisfine|ACremovethis|ADandleavetherest', more line here,3)" */

示例输出:

"So |AAthis|ABpart|is fine, as is this -- but remove ('|replaced|ABthisisfine|ACremovethis|ADandleavetherest', more line here,3)"
"And |AAthis|ABpart|is fine, as is this /* but remove
('|replaced|ABthisisfine|ACremovethis|ADandleavetherest', more line here,3)" */

【讨论】:

  • @mbourgon - 如果答案有助于解决您的问题,请务必投票。
  • 不得不稍微改变一下——我需要转义几个与你不同的字符。一切顺利,再次感谢!我用"(\/\*(?:(?!\*\/)[\S\s])*?)(\|AA[^\|]*)([\|]+(?:(?!\*\/)[\S\s])*?\*\/)|(--.*?)(\|AA[^\|]*)([\|]+(.*))"
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-12-08
  • 2017-11-16
  • 2015-09-17
  • 2020-08-05
  • 2020-07-18
  • 2012-05-12
  • 2012-05-18
相关资源
最近更新 更多