英文:
Powershell - get content between 2 strings in multiple lines
问题
Desired output:
<media src="C:\Users\user\Downloads\Katzianerova vojna.mp3"/>
<media src="C:\Users\user\Downloads\Rat i mir u povijesti III- dio.mp3"/>
Actual output:
<media src="C:\Users\user\Downloads\Katzianerova vojna.mp3"/> <media src="C:\Users\user\Downloads\Rat i mir u povijesti III- dio.m
p3"/>
英文:
I have file 1.wpl:
<?wpl version="1.0"?>
<smil>
<head>
<meta name="Generator" content="Microsoft Windows Media Player -- 12.0.22621.1"/>
<meta name="ItemCount" content="2"/>
<title>Untitled playlist</title>
</head>
<body>
<seq>
<media src="C:\Users\user\Downloads\Katzianerova vojna.mp3"/>
<media src="C:\Users\user\Downloads\Rat i mir u povijesti III- dio.mp3"/>
</seq>
</body>
</smil>
I want to get content between <seq>
and </seq>
in multiple lines:
Desired output:
<media src="C:\Users\user\Downloads\Katzianerova vojna.mp3"/>
<media src="C:\Users\user\Downloads\Rat i mir u povijesti III- dio.mp3"/>
Have this code which gives me output in single line:
$fileName = "C:\Users\user\Music\Playlists.wpl"
#Get content from file
$file = Get-Content $fileName
#Regex pattern to compare two strings
$pattern = "<seq>(.*?)</seq>"
#Perform the opperation
$results = [regex]::Match($file,$pattern).Groups[1].Value -split [System.Environment]::NewLine
return $results
Actual output:
<media src="C:\Users\user\Downloads\Katzianerova vojna.mp3"/> <media src="C:\Users\user\Downloads\Rat i mir u povijesti III- dio.m
p3"/>
答案1
得分: 2
没有使用正则表达式的理由,因为您的内容是有效的XML:
($xml = [xml]::new()).Load('C:\Users\user\Music\Playlists.wpl')
$xml.SelectNodes('smil/body/seq/media') | ForEach-Object OuterXml
# 输出:
# <media src="C:\Users\user\Downloads\Katzianerova vojna.mp3" />
# <media src="C:\Users\user\Downloads\Rat i mir u povijesti III- dio.mp3" />
或者在XPath中使用通配符:
($xml = [xml]::new()).Load('C:\Users\user\Music\Playlists.wpl')
$xml.SelectNodes("//seq/*") | ForEach-Object OuterXml
英文:
There is literally no reason to use regex when what you have is valid XML:
($xml = [xml]::new()).Load('C:\Users\user\Music\Playlists.wpl')
$xml.SelectNodes('smil/body/seq/media') | ForEach-Object OuterXml
# Outputs:
# <media src="C:\Users\user\Downloads\Katzianerova vojna.mp3" />
# <media src="C:\Users\user\Downloads\Rat i mir u povijesti III- dio.mp3" />
Or using wildcard in the XPath:
($xml = [xml]::new()).Load('C:\Users\user\Music\Playlists.wpl')
$xml.SelectNodes("//seq/*") | ForEach-Object OuterXml
答案2
得分: 1
你可以尝试使用一个 "switch"
Get-Content -Path "C:\Users\user\Music\Playlists.wpl" |
ForEach-Object {
switch -Regex ($_) {
'\<seq\>$' {
$break = 1
}
'\<\/seq\>$' {
$break = 0
}
default {
if ($break -eq 1) {
$_ -replace '^\s+'
}
}
}
}
英文:
You could try using a "switch"
Get-Content -Path "C:\Users\user\Music\Playlists.wpl" |
ForEach-Object {
switch -Regex ($_) {
'\<seq\>$' {
$break = 1
}
'\<\/seq\>$' {
$break = 0
}
default {
if ($break -eq 1) {
$_ -replace '^\s+'
}
}
}
}
答案3
得分: 0
找到解决方案:
$fileContent = Get-Content -Path "C:\Users\user\Music\Playlists.wpl" -Raw
$regexPattern = "(?s)<seq>(.*?)</seq>"
$matches = [regex]::Match($fileContent, $regexPattern)
if ($matches.Success) {
$seqContent = $matches.Groups[1].Value
$lines = $seqContent -split "`n"
$output = ($lines | Where-Object { $_.Trim() -ne '' }) -join "`n"
Write-Output $output
} else {
Write-Output "在文件中未找到 <seq> 内容。"
}
英文:
found solution:
$fileContent = Get-Content -Path "C:\Users\user\Music\Playlists.wpl" -Raw
$regexPattern = "(?s)<seq>(.*?)</seq>"
$matches = [regex]::Match($fileContent, $regexPattern)
if ($matches.Success) {
$seqContent = $matches.Groups[1].Value
$lines = $seqContent -split "`n"
$output = ($lines | Where-Object { $_.Trim() -ne '' }) -join "`n"
Write-Output $output
} else {
Write-Output "No <seq> content found in the file."
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论