带 powershell 的正则表达式

Regex with powershell

我正在编写一个 powershell 脚本,如果两个用户在从 plex 服务器播放时使用相同的用户名但不同的 IP,则通知我。

我设法 xml 显示当时正在运行的当前连接。

我需要做的是想出一个正则表达式,在其中提取用户 ID 和 IP 地址,然后我可以进行搜索以查看是否存在重复的用户 ID 以及具有不同 IP 的用户 ID。

我设法找到了 IP 地址的正则表达式 '\b\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3 }\b'

但是也很难从中提取用户 ID。请注意,用户 ID 将始终是数字,但没有设置限制。

这里是数据的例子

<User id="13456" title="usersmith" />
<Player address="2.2.2.2" device="Windows" machineIdentifier="a9b222ef940" 

我会将其解析为 xml,例如:

#$xml = [xml](Get-Content myfile.xml)
#$xml = [xml](Invoke-WebRequest ... whatever).Content
$xml = [xml]@"
<?xml version="1.0" encoding="utf-8"?>
<root>
<User id="13456" title="usersmith" />
<Player address="2.2.2.2" device="Windows" machineIdentifier="a9b222ef940" />
</root>
"@

$xml.root.user.id

但是如果您真的想要正则表达式,请尝试@heemayl 的解决方案,但行首 ^-anchor 可能不适合您的真实 xml-data(在那种情况下您已经提供了错误的样本数据)。例如:

if('User id <User id="1354" thumb="plex.tv/users/a51d"; title="bob" />' -match '<User\s+id="([^"]+)"') { $Matches[1] }