【问题标题】:Powershell using Regex finding a string within a string in XMLPowershell使用正则表达式在XML中的字符串中查找字符串
【发布时间】:2012-10-13 00:11:54
【问题描述】:

我有一个 xml 文件,我只需要从主字符串中读取一个特定的子字符串。 xml 文件如下所示:

<?xml version="1.0" encoding="utf-8"?>
<Report Version="10.0">
<Entities>
<Entity Name="\\sharing\Data\SB\**appname**\wcf\Utilitysvc\bin\svcUtility.Host.dll">
<Mods>
<Mod Name="svcUtility.Host.dll" AssemblyVersion="1.0.2000.001">
<Fields>
<Field Name="TIndex" Value="100" />            
<Field Name="Vindex" Value="200" />
</Fields>
</Mod>
</Mods>
</Entity>
</Entities>
</Report>

这个xml中的主要字符串是-

<Entity Name="\\sharing\Data\SB\**appname**\wcf\Utilitysvc\bin\svcUtility.Host.dll">

我只需要从中打印“appname”。

我可以使用什么条件逻辑在 powershell 中使用正则表达式来打印它?并且它不需要总是在 appname 之后是 \wcf .. 它可以是任何基于 dll 路径的东西。例如,它可以是:

<Entity Name="\\sharing\Data\*SB*\**appname**\**Web**\Utilitysvc\bin\svcUtility.Host.dll">

<Entity Name="\\sharing\Data\*SB*\*DEVCS*\**appname**\**junk**\Utilitysvc\bin\svcUtility.Host.dll">

我可以有一个通用的 select -string 方式吗? 这个也需要测试一下..

谢谢,
阿什什

【问题讨论】:

    标签: xml regex powershell substring


    【解决方案1】:

    这是一种方式:

    $xml = [xml](get-content .\my.xlm )
    
     ($xml.Report.Entities.Entity.name | 
    % { [regex]::matches($_, 'SB\\(.*)\\wcf') } |
     select -expand groups)[1].value
    

    没有 [regex] .net 方法:

    ($xml.Report.Entities.Entity.name |
    select-string 'SB\\(.*)\\wcf' -AllMatches | select -ExpandProperty matches |
    select -ExpandProperty groups)[1].value
    

    编辑:

    根据您的上一条评论尝试此模式:

     ($xml.Report.Entities.Entity.name |
        select-string '(?<=\\Data\\.*\\)[^\\]*' -AllMatches |
        select -ExpandProperty matches |
        select -ExpandProperty groups)[0].value
    

    【讨论】:

    • 您使用正则表达式的方法有效。但 \wcf 可以根据 dll 路径进行更改。我可以有一个通用的 select -string 方式吗?此外,在 appname 之前,它可以是 SB 或 AB .. 我如何使用 OR 进行测试?
    • @Christian.. 抱歉,它可能是应用程序名之前的 SB 或 DEVCS .. 我尝试使用 \(.*)\\.*' 但它在应用程序名之后标记了所有内容..
    • 我有大约 300 个奇怪的 xml 文件,其中路径可以根据 dll 更改。所以,除了 wcf 或 web 它可以是 A-Z 中的任何内容:(
    • @ashishg。用正则表达式lookbehind断言重新编辑了我的答案......这工作希望​​路径总是在里面......
    • 它部分工作.. 我怎么能省略出现在 appname 之后的任何内容.. 它可以是来自 AZ 的任何内容,不一定只有 wcf 或 web。
    【解决方案2】:

    您可以做到这一点,而无需正则表达式的复杂性,拆分路径并获取第 5 个元素(听起来像电影名称):

    [xml]$xml = @"
    <?xml version="1.0" encoding="utf-8"?>
    <Report Version="10.0">
    <Entities>
    <Entity Name="\\sharing\Data\SB\**appname**\wcf\Utilitysvc\bin\svcUtility.Host.dll">
    <Mods>
    <Mod Name="svcUtility.Host.dll" AssemblyVersion="1.0.2000.001">
    <Fields>
    <Field Name="TIndex" Value="100" />            
    <Field Name="Vindex" Value="200" />
    </Fields>
    </Mod>
    </Mods>
    </Entity>
    </Entities>
    </Report>
    "@
    
    $xml.Report.Entities.Entity.Name.split('\')[5]
    
    **appname**
    

    【讨论】:

    • appname 不必总是在同一位置。请参阅我的问题中的编辑。如何在 if 条件下使用您的代码。例如:如果 (Entity Name="\\sharing\Data\SB**appname**\wcf\Utilitysvc...) 那么 $xml.Report.Entities.Entity.Name.split('\')[5]否则 $xml.Report.Entities.Entity.Name.split('\')[6]
    猜你喜欢
    • 2011-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-26
    • 2013-06-23
    相关资源
    最近更新 更多