2012-10-23 2 views
1

XML 파일이있어서 기본 문자열에서 특정 하위 문자열 만 읽어야합니다. XML 파일은 다음과 같습니다Regex를 사용하여 Powershell 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에서 regex를 사용하여 이것을 인쇄하려면 어떤 조건 논리를 사용할 수 있습니까? 그리고 그것은 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"> 

내가 일반적인 선택 -String 방법이 있습니다 : 같은 예를 들어, 수 있는가? ,

감사합니다 .. 물론이 테스트
인 Ashish

당신은 정규식의 복잡성없이 경로를 분할 그렇게하고 (영화 이름처럼 소리) 5 요소를 잡을 수

답변

0

필요합니다

[정규식] .NET 방법없이

$xml = [xml](get-content .\my.xlm) 

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

:

[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** 
+0

을 그 ** APPNAME ** 같은 position.Please에 항상 내 질문에 편집을 참조 . if 조건으로 코드를 사용하려면 어떻게해야합니까? 예를 들어 : (엔터티 이름 = "\\ 공유 \ 데이터 \ SB \ ** appname ** \ wcf \ Utilitysvc ...) 경우 $ xml.Report.Entities.Entity.Name.split ('\') [5 ] else $ xml.Report.Entities.Entity.Name.split ('\') [6] –

2

이 는 방법

($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 
+0

regex를 사용하는 메서드가 작동하지만 \ wcf가 dll 경로를 기반으로 변경 될 수 있습니다. 문자열 방법? 또한 appname 전에 그것은 SB 또는 AB 수 있습니다. 또는 OR 함께이 테스트 할 수 있습니다. –

+0

@Christian .. 미안 appname .. 전에 SB 또는 DEVCS 중 하나가 될 수 및 \\ 시도한 \\ (. *) \\. * '그러나 appname 뒤에 모든 것을 따라 태그가 붙습니다. –

+0

나는 경로가 dll에 따라 달라질 수있는 약 300 개의 홀수 xml 파일을 가지고 있습니다 ... wcf 또는 웹을 제외하고는 AZ : ( –

관련 문제