PowerShell文本拆分问题:如何从TXT文件提取Brackets版本信息
Extract Specific Version String from TXT File with PowerShell
Hey there! Let's get that "Release 1.11.6" string extracted smoothly. You were on the right track with Where-Object—here's how to finish the code and get exactly what you need:
Option 1: Split by Delimiter
This is straightforward since your text uses # as a clear separator:
Get-Content -Path thisfile.txt | Where-Object { $_ -match '^Brackets#' } | # Filter lines starting with "Brackets#" ForEach-Object { ($_ -split '#')[1] } # Split the line by # and grab the 2nd element (index 1)
Breakdown:
^Brackets#uses a regex anchor (^) to ensure we only match lines that start with "Brackets#" (avoids accidental matches elsewhere in the text).- Splitting the line with
-split '#'turns it into an array like["Brackets", "Release 1.11.6", "Path-to-Brackets"]—index 1 is your target value.
Option 2: Regex Capture Group
If you prefer a more flexible approach (great if your formatting might vary slightly):
Get-Content -Path thisfile.txt | Where-Object { $_ -match '^Brackets#(.*?)#' } | # Capture text between the first two #s ForEach-Object { $matches[1] } # Extract the captured group
Breakdown:
- The regex
^Brackets#(.*?)#captures everything between the first#after "Brackets" and the next#. The(.*?)is a non-greedy match, so it stops at the first subsequent#. $matches[1]pulls out the content captured by the parentheses.
Quick One-Liner (if only one matching line exists)
If you’re sure there’s only one line starting with "Brackets#" in the file, you can simplify it further:
(Get-Content thisfile.txt | Where-Object { $_ -match '^Brackets#' }) -split '#' | Select-Object -Index 1
内容的提问来源于stack exchange,提问作者Jp Morgan
相关产品推荐
相关产品推荐

