使用powershell的正则表达式

时间:2016-05-10 11:24:00

标签: regex powershell plex

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

我设法获得当时正在冒充的当前连接的xml显示。

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

我设法找到了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" 

1 个答案:

答案 0 :(得分:1)

我会将其解析为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的解决方案,除了行首^ - 锚点,这可能不适合你的真实xml数据(在这种情况下你是&ve;提供了不良的抽样数据)。例如:

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