Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.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
Arrays 需要帮助获得正确正则表达式的新手_Arrays_Regex_String_Powershell - Fatal编程技术网

Arrays 需要帮助获得正确正则表达式的新手

Arrays 需要帮助获得正确正则表达式的新手,arrays,regex,string,powershell,Arrays,Regex,String,Powershell,我有来自powershell网站的证书信息,它们通常是这样的 CN=Google Internet Authority G3, O=Google Trust Services, C=US 我需要帮助获得正确的正则表达式,以便只在CN=之后获取信息,直到逗号 第二个问题是,我得到的一些证书只有一个CN=,因此末尾没有逗号,所以看起来像 我如何使用正则表达式来捕获这两种情况 以下是我认为行之有效的方法: $cert.Issuer -match "CN=(?<issuer>.*(?=,)

我有来自powershell网站的证书信息,它们通常是这样的

CN=Google Internet Authority G3, O=Google Trust Services, C=US
  • 我需要帮助获得正确的正则表达式,以便只在CN=之后获取信息,直到逗号

  • 第二个问题是,我得到的一些证书只有一个CN=,因此末尾没有逗号,所以看起来像

  • 我如何使用正则表达式来捕获这两种情况

    以下是我认为行之有效的方法:

    $cert.Issuer -match "CN=(?<issuer>.*(?=,))"
        Write-Host $Matches['issuer']
    >> Google Internet Authority G3, O=Google Trust Services
    
    $cert.Issuer -match "CN=(?<issuer>.*)?,?\s"
        Write-Host $Matches['issuer']
    >> Google Internet Authority G3, O=Google Trust Services,
    
    $cert.Issuer -match "CN=(?<issuer>.*),|\s"
        Write-Host $Matches['issuer']
    >> Google Internet Authority G3, O=Google Trust Services
    
    它是否有一个逗号,然后是更多信息,或者没有逗号,并且是字符串的结尾


    谢谢

    如果文本本身不能包含逗号,则可以使用否定字符类来匹配除逗号以外的任何字符。然后匹配在命名的捕获组
    issuer

    CN=(?<issuer>[^,]+)
    
    解释

    • CN=
      逐字匹配
    • (?
      命名组
      发行人
      • *?
        匹配除换行非贪婪字符以外的任何字符(尽可能少)
    • 关闭命名组
    • (?:
      非捕获组
      • 匹配逗号和空格
      • |
      • $
        断言字符串的结尾
    • 关闭命名组

    |

    在尝试
    $cert.Issuer-match“CN=(?*),?\s”
    时,问题是使用贪婪匹配
    *
    ,后跟
    ,?
    。贪婪匹配将只匹配
    CN=
    之后的剩余行,直到最后一次
    \s
    匹配为止。
    ,?
    表示可能有一个或零个
    字符,从而导致下一个字符匹配,而不管它是
    。将您的尝试修改为以下内容将产生您想要的结果

    $cert.Issuer -match "CN=(?<issuer>.*?),\s"
    $matches['issuer']
    Google Internet Authority G3
    
    另一种方法是使用.NET正则表达式类中的
    Match()
    方法,该方法返回
    [System.Text.RegularExpressions.Match]
    对象。您可以访问该对象的
    属性以返回所需的数据

    [regex]::Match($cert.Issuer,"(?<=CN=).*?(?=,\s*O=)","IgnoreCase").Value
    

    [regex]:Match($cert.Issuer,”(?作为两步操作(首先匹配regex,然后检查其结果)的替代方法,提供了一个简洁的解决方案:

    PS> 'CN=Google Internet Authority G3, O=Google Trust Services, C=US' -replace
          '.*\bCN=([^,]+).*', '$1'
    
    Google Internet Authority G3
    

    关键是让正则表达式匹配整个输入字符串,并捕获捕获组中感兴趣的子字符串(
    ([^,]+)
    ),它在替换字符串中可以被引用为
    $1

    ,这是我在regex标记中看到的一个更好的问题。做得好。只需添加相同的行,也可以使用正向查找-(?太棒了!效果很好,我对regex还是新手,不知道否定类。除了regex演示之外,最好使用什么资源?为了确保我理解,我们说字符串必须以CN=开头,然后我们正在创建一个组,该组将具有?零个或更多字符,并具有分类(不知道正确的词)的,那么我们在[]中使用^要说它不能有逗号,\r返回字符或换行符。\r加号有什么作用?这使它变得贪婪,也使它变得贪婪,所以我们可以尽可能多地使用,但最多只能使用逗号、返回符或换行符?@VirtualPenman我添加了一个解释。
    +/code>是一个匹配1+次的加号。
    ^
    之间的
    []
    是a,将匹配除所列字符以外的任何字符。非常感谢,如果我可以加倍投票,我会的。
    CN=(?<issuer>.*?)(?:, |$)
    
    $cert.Issuer -match "CN=(?<issuer>.*?),\s"
    $matches['issuer']
    Google Internet Authority G3
    
    ($cert.Issuer -split "CN=|,\s*O=")[1]
    
    [regex]::Match($cert.Issuer,"(?<=CN=).*?(?=,\s*O=)","IgnoreCase").Value
    
    PS> 'CN=Google Internet Authority G3, O=Google Trust Services, C=US' -replace
          '.*\bCN=([^,]+).*', '$1'
    
    Google Internet Authority G3