Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 将正确转义的引号与DOTNET正则表达式匹配_.net_Regex_Powershell - Fatal编程技术网

.net 将正确转义的引号与DOTNET正则表达式匹配

.net 将正确转义的引号与DOTNET正则表达式匹配,.net,regex,powershell,.net,Regex,Powershell,在我的PowerShell代码中可以避免注入用户提供的字符串。虽然我有正确转义它的代码(复制每个引号,带单引号字符串的powershell接受5个不同的引号字符,包括智能引号,现在让我们假设它接受' 我想做的是让一个正则表达式告诉我字符串是否正确转义,转义是通过将引号加倍来完成的,因此字符串如下所示 hello ' there 这段时间很糟糕 hello '' there 是安全的 然而,3个引号(或5或7等)也是不好的 hello ''' there 这也是危险的 因此,我试图找到一个正

在我的PowerShell代码中可以避免注入用户提供的字符串。虽然我有正确转义它的代码(复制每个引号,带单引号字符串的powershell接受5个不同的引号字符,包括智能引号,现在让我们假设它接受' 我想做的是让一个正则表达式告诉我字符串是否正确转义,转义是通过将引号加倍来完成的,因此字符串如下所示

hello ' there
这段时间很糟糕

hello '' there
是安全的

然而,3个引号(或5或7等)也是不好的

hello ''' there
这也是危险的

因此,我试图找到一个正则表达式,它可以验证字符串是否正确转义,因为不存在奇数单引号模式

我知道像这样的标准正则表达式计数组是不可能的,但对于dotnet捕获组,我希望能做类似的事情

('\b(?<DEPTH>)|\b'(?<-DEPTH>)|[^']*)*(?(DEPTH)(?!))
('\b(?)\b'(?)(^']*)*(?(深度)(?!)

但是我不能让它工作。

为什么不简单地用两个替换一个呢


为什么不简单地将一个“替换为两个”


就因为是你,@klumsy:

"(?ix:                 # ignore whitespace and comments
    ^                  # start at the beginning
    (?(D)              # if 'D' is defined...
        (?<-D>')       #    match a quote and undefine D
        |              # otherwise
        (?:
            (?<D>')    #    match a quote and define D
            |
            [^']       #    or match anything else
            )
    )+                 # as many times as we can
    (?(D)              # if 'D' is STILL defined...
        ($!)           #    then don't match
        |              # otherwise
        [^']*          #    match anything except '
    )$                 # all the way to the end
)"
(?ix:#忽略空格和注释 ^#从头开始 (?(D)#如果定义了“D”。。。 (?')#匹配引号并取消定义D |否则 (?: (?')#匹配引号并定义D | [^']#或匹配其他任何内容 ) )+#尽可能多地 (?(D)#如果仍然定义了“D”。。。 ($!)#那么就不匹配了 |否则 [^']*#匹配除' )$#一直到最后 )" 这将只匹配始终有引号成对的字符串,但不匹配出现单引号或奇数引号的字符串。据我所知,仅适用于.Net正则表达式


当然,您可以省略第一行和最后一行,只要删除所有空格和注释。

就因为是您,@klumsy:

"(?ix:                 # ignore whitespace and comments
    ^                  # start at the beginning
    (?(D)              # if 'D' is defined...
        (?<-D>')       #    match a quote and undefine D
        |              # otherwise
        (?:
            (?<D>')    #    match a quote and define D
            |
            [^']       #    or match anything else
            )
    )+                 # as many times as we can
    (?(D)              # if 'D' is STILL defined...
        ($!)           #    then don't match
        |              # otherwise
        [^']*          #    match anything except '
    )$                 # all the way to the end
)"
(?ix:#忽略空格和注释 ^#从头开始 (?(D)#如果定义了“D”。。。 (?')#匹配引号并取消定义D |否则 (?: (?')#匹配引号并定义D | [^']#或匹配其他任何内容 ) )+#尽可能多地 (?(D)#如果仍然定义了“D”。。。 ($!)#那么就不匹配了 |否则 [^']*#匹配除' )$#一直到最后 )" 这将只匹配始终有引号成对的字符串,但不匹配出现单引号或奇数引号的字符串。据我所知,仅适用于.Net正则表达式


当然,您可以省略第一行和最后一行,只要您删除所有空格和注释。

我已经有了大量代码,可以在其他地方运行这些代码来清理输入。但是在这种情况下,我没有机会进行预处理,只能根据正则表达式对其进行过滤,如果它通过了该正则表达式,那么它将被删除我已经有了大量的代码,可以在别处运行来清理输入。但是在这种情况下,我没有机会进行预处理,只能根据正则表达式对其进行过滤,如果它通过了正则表达式,那么它将直接注入代码。这是压缩版本^(?(D)(?)|(?:(?)|[^'])+(?(D)($!)|[^']*)$这是压缩版本^(?(D)(?)|(?:(?)|[^'])+(?(D)($!)|[^']*)$