有一个文件C:\ Density.txt,其中定期写入四个值。 为了进行处理和进一步分析,我只需要第四个符号Density:。
文件处理方法如下。 是否可以使用正则表达式获得“密度:”值?
文件示例英文:
Solid density
Mass in the air:
23.384 (1) g
Mass in liquid:
23.383 (3) g
Solid volume:
0.001 cm3
Density:
1111.586 g / cm3
==============
Solid density
Mass in the air:
23.384 (1) g
Mass in liquid:
23.383 (3) g
Solid volume:
0.001 cm3
Density:
1112.586 g / cm3
==============
Solid density
Mass in the air:
23.384 (1) g
Mass in liquid:
23.383 (3) g
Solid volume:
0.001 cm3
Density:
1113.586 g / cm3
==============
我尝试添加您的解决方案代码,但出现错误。我需要锁定文件(ScalesM)进行读写。获取值后,需要将结果逐行写入另一个文件OutFile中。有什么办法可以修改您的解决方案代码?
$ScalesM = [System.io.File]::Open('C:\DensityNT.txt', 'Open', 'ReadWrite', 'None')
$OutFile = [System.io.File]::Open('C:\\InfinityDensity.txt', 'append', 'Write', 'None')
$ScalesM2 = New-Object System.IO.StreamReader($ScalesM)
$text = $text + $ScalesM2.ReadToEnd()
# Next sring Give my the Error -
$text = ($text | Select-String -Pattern "[0-9\.].+?(?=( g\/cm3))" -AllMatches).Matches.Value
$data = $enc.GetBytes($text)
$OutFile.write($data,0,$data.length)
$ScalesM.SetLength(0)
$ScalesM.Close()
$OutFile.Close()
答案 0 :(得分:1)
您可以使用正向前瞻:
$Text = Get-Content C:\Density.txt
($Text | Select-String -Pattern "[0-9\.].+?(?=( g \/ cm3))" -AllMatches).Matches.Value
输出:
1111.586
1112.586
1113.586
答案 1 :(得分:0)
这应该做到:
$txt = Get-Content 'PATH TO THE DENSITY.TXT FILE'
$regex = [regex] '(?m)Density:\s+(?<density>.+)'
$match = $regex.Match($txt)
while ($match.Success) {
$match.Groups['density'].Value
$match = $match.NextMatch()
}
将输出
1111.586 g / cm3 1112.586 g / cm3 1113.586 g / cm3
在我的计算机上,值为Плотность:
的正则表达式无法正常工作。
由于您想要的值是以g/cm3
或g / cm3
结尾的值,因此下面的代码将它们同时用于英语和俄语版本:
$txt = Get-Content 'PATH TO THE DENSITY.TXT FILE' -Encoding UTF8
$regex = [regex]'(?<density>\d+(?:\.\d+)?\s+g\s*/\s*cm3)'
$match = $regex.Match($txt)
$result = while ($match.Success) {
$match.Groups['density'].Value
$match = $match.NextMatch()
}
# output on screen:
$result
# output to file
$result | Set-Content 'D:\Densities.txt'
正则表达式详细信息:
(?<density> Match the regular expression below and capture its match into backreference with name “density” \d Match a single digit 0..9 + Between one and unlimited times, as many times as possible, giving back as needed (greedy) (?: Match the regular expression below \. Match the character “.” literally \d Match a single digit 0..9 + Between one and unlimited times, as many times as possible, giving back as needed (greedy) )? Between zero and one times, as many times as possible, giving back as needed (greedy) \s Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.) + Between one and unlimited times, as many times as possible, giving back as needed (greedy) g Match the character “g” literally \s Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.) * Between zero and unlimited times, as many times as possible, giving back as needed (greedy) \/ Match the character “/” literally \s Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.) * Between zero and unlimited times, as many times as possible, giving back as needed (greedy) cm3 Match the characters “cm3” literally )
答案 2 :(得分:0)
感谢您的帮助。 完整的解析度如下:
$TValue = ""
$enc = [system.Text.Encoding]::UTF8
$NL = [System.Environment]::NewLine
while ($countI=1) {
try {
$ScalesM = [System.io.File]::Open('C:\IN.txt', 'Open', 'ReadWrite', 'None')
$OutFile = [System.io.File]::Open('C:\OUT.txt', 'append', 'Write', 'None')
$ScalesM2 = New-Object System.IO.StreamReader($ScalesM)
$text = $ScalesM2.ReadToEnd()
[regex]::matches($text, ':\s*(\d+\.?\d*)\s+g\s*/\s*cm3') | %{
$TValue = $TValue + $_.Groups[1].value + $NL
}
$data = $enc.GetBytes($TValue)
$OutFile.write($data,0,$data.Length)
$ScalesM.SetLength(0)
$ScalesM.Close()
$OutFile.Close()
Wait-Event -Timeout 1
}
catch {
echo ″Some error was found.″
$ScalesM.Close()
$OutFile.Close()
Wait-Event -Timeout 0.5
}
}