PowerShell:在两个单词之间查找文本

PowerShell: Find Text between two Words

我要查一下,tomcat的connector port 8080中是否配置了maxThreadsand/ormaxConnections

示例:

<Connector port="8080" protocol="HTTP/1.1"
       maxThreads="600"
       maxConnections="3000"
           connectionTimeout="20000"
           redirectPort="443" />

我用 regex 试过了,但很失败。

$file = Get-Content "D:\Program Files\Apache Software Foundation\Tomcat 7.0\conf\server.xml"
$pattern = "(?<=.*protocol=""HTTP/1.1"")\w+?(?=connectionTimeout=""20000"".*)"
$Opts = [Regex]::Match($file, $pattern)

提前致谢

这很简单

PS>[xml]$x=gc "D:\Program Files\Apache Software Foundation\Tomcat 7.0\conf\server.xml" 
PS>$x.xml.Connector.maxConnections   
3000         
PS>$x.xml.Connector.maxThreads 
600            

如果你有多个连接器,你可以做

$x.xml.Connector | ?{ $_.port -eq "8080"} |select maxthreads