我们有一个从mac用户计算机导出的大型.vcf。
导出联系人的过程导致单个.vcf将所有联系人聚集到一个文件中。我使用notepad ++将“BEGIN:”的所有实例替换为“\ nBEGIN:”,以便我今晚可以睡觉。
计划是将每个匹配到我的reg表达式中,然后将每个字符串输出到许多唯一命名的.vcf文件
(我打算稍后将字符串“BEGIN:VCARD”和“END:VCARD”添加回每个文件的开头和结尾。)
这是我们正在处理的数据:
BEGIN:VCARD
VERSION:3.0
PRODID:-//Apple Inc.//Mac OS X 10.13.4//EN
N:;;;;
TEL;type=CELL;type=VOICE;type=pref:+18005555555
UID:3fe8e0-421c-4c6a-bfa-38c75df8c07
X-ABUID:3FE8490-421C-4C6A-B2FA-38C15DF8C07:ABPerson
END:VCARD
BEGIN:VCARD
VERSION:3.0
PRODID:-//Apple Inc.//Mac OS X 10.13.4//EN
N:;<blah@company.org>;;;
FN:<blah@company.org>
item1.EMAIL;type=INTERNET;type=pref:blah@company.org
item1.X-ABLabel:_$!<Other>!$_
UID:5ad596-a879-4c98-9f56-2ef90efe32f
X-ABUD:DB5C20C-6DFC-450F-A752-D57964F6F3A:ABPerson
END:VCARD
...
我接近下面的代码,但它只返回第一场比赛
$String = cat C:\temp\contacts.txt
$Regex = [Regex]::new("(?<=BEGIN:VCARD)(.*?)(?=END:VCARD)")
$Match = $Regex.Match($String)
if($Match.Success)
{
$Match.Value
}
我需要它来解析整个字符串并找到像这样的所有匹配:
$matches_found = @()
cat myfile.txt | %{
if ($_ -match '(?<=BEGIN:VCARD)(.*?)(?=END:VCARD)'){
$matches_found += $matches[1]
}
}
但是当我把我的正则表达式放到这个代码中时,它找不到任何匹配
答案 0 :(得分:2)
您只需在发布的每个代码块中要求一个匹配项。 你想要使用RegEx Matches。
这可以让你了解你的目标:
$VCardData = @'
BEGIN:VCARD
VERSION:3.0
PRODID:-//Apple Inc.//Mac OS X 10.13.4//EN
N:;;;;
TEL;type=CELL;type=VOICE;type=pref:+18005555555
UID:3fe8e0-421c-4c6a-bfa-38c75df8c07
X-ABUID:3FE8490-421C-4C6A-B2FA-38C15DF8C07:ABPerson
END:VCARD
BEGIN:VCARD
VERSION:3.0
PRODID:-//Apple Inc.//Mac OS X 10.13.4//EN
N:;<blah@company.org>;;;
FN:<blah@company.org>
item1.EMAIL;type=INTERNET;type=pref:blah@company.org
item1.X-ABLabel:_$!<Other>!$_
UID:5ad596-a879-4c98-9f56-2ef90efe32f
X-ABUD:DB5C20C-6DFC-450F-A752-D57964F6F3A:ABPerson
END:VCARD
'@
# Use RegEx match to search for strings across line breaks.
$VcardRegEx = '(?s)(?<=BEGIN:VCARD).*?(?=END:VCARD)'
# Select all matches
[RegEx]::Matches($VCardData,$VcardRegEx).Value
#results
VERSION:3.0
PRODID:-//Apple Inc.//Mac OS X 10.13.4//EN
N:;;;;
TEL;type=CELL;type=VOICE;type=pref:+18005555555
UID:3fe8e0-421c-4c6a-bfa-38c75df8c07
X-ABUID:3FE8490-421C-4C6A-B2FA-38C15DF8C07:ABPerson
VERSION:3.0
PRODID:-//Apple Inc.//Mac OS X 10.13.4//EN
N:;<blah@company.org>;;;
FN:<blah@company.org>
item1.EMAIL;type=INTERNET;type=pref:blah@company.org
item1.X-ABLabel:_$!<Other>!$_
UID:5ad596-a879-4c98-9f56-2ef90efe32f
X-ABUD:DB5C20C-6DFC-450F-A752-D57964F6F3A:ABPerson
根据OP的后续问题进行更新
# How many records are in the set
([RegEx]::Matches($VCardData,$VcardRegEx).Value).Count
# Results
2
# Output each record as a separate file
# Set the counter
$VCardCounter = 0
# Loop through the dataset and output to a new file for each
ForEach($Vcard in ([RegEx]::Matches($VCardData,$VcardRegEx).Value))
{
$VCardFileName = 'VCard' + ++$VCardCounter + ".txt"
New-Item -Path $pwd -ItemType File -Name $VCardFileName
Add-Content -Value $Vcard -Path "$pwd\$VCardFileName"
}
Get-ChildItem -Path "$pwd\Vcard*"
# List the new files
Directory: D:\Scripts
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 03-Jun-18 15:36 209 VCard1.txt
-a---- 03-Jun-18 15:36 286 VCard2.txt
# Review the contents of the new files
Get-Content (Get-ChildItem -Path "$pwd\Vcard*")
# Results
VERSION:3.0
PRODID:-//Apple Inc.//Mac OS X 10.13.4//EN
N:;;;;
TEL;type=CELL;type=VOICE;type=pref:+18005555555
UID:3fe8e0-421c-4c6a-bfa-38c75df8c07
X-ABUID:3FE8490-421C-4C6A-B2FA-38C15DF8C07:ABPerson
VERSION:3.0
PRODID:-//Apple Inc.//Mac OS X 10.13.4//EN
N:;<blah@company.org>;;;
FN:<blah@company.org>
item1.EMAIL;type=INTERNET;type=pref:blah@company.org
item1.X-ABLabel:_$!<Other>!$_
UID:5ad596-a879-4c98-9f56-2ef90efe32f
X-ABUD:DB5C20C-6DFC-450F-A752-D57964F6F3A:ABPerson
答案 1 :(得分:0)
此PowerShell脚本
BEGIN:VCARD
。NoUID#0000.vcf
都带有递增计数器编辑简化版本,只有一个外部文件名计数器
## Q:\Test\2018\06\02\SO_50659915.ps1
$InFile = '.\sample.vcf'
$Delimiter = 'BEGIN:VCARD'
$Split = "(?!^)(?=$Delimiter)"
(Get-Content $InFile -Raw) -split $Split | ForEach-Object {$I=0}{
$I++
$_ | Out-File -FilePath ("Whatever{0:0000}.vcf" -f $I) -Encoding UTF8
}
## Q:\Test\2018\06\02\SO_50659915.ps1
$InFile = '.\sample.vcf'
$Delimiter = 'BEGIN:VCARD'
# If the Delimiter contains chars that would be interpreted as special RE chars
# they need to be escaped, either manually or with the following command
# $Escaped = [regex]::Escape($Delimiter)
$Split = "(?!^)(?=$Delimiter)"
(Get-Content $InFile -Raw) -split $Split | ForEach-Object {$I=0}{
if ($_ -match 'UID:(?<UID>[0-9a-f\-]{32})'){
$_ | Out-File -FilePath ($Matches.UID+".vcf") -Encoding UTF8
} else {
$I++
$_ | Out-File -FilePath ("NoUID#{0:0000}.vcf" -f $I) -Encoding UTF8
}
}
示例结果输出:
> ls
Directory: Q:\Test\2018\06\02
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2018-06-03 20:05 236 3fe8e0-421c-4c6a-bfa-38c75df8c07.vcf
-a---- 2018-06-03 20:05 311 5ad596-a879-4c98-9f56-2ef90efe32.vcf
-a---- 2018-06-03 20:05 236 NoUID#0001.vcf
-a---- 2018-06-03 20:05 311 NoUID#0002.vcf
-a---- 2018-06-02 21:45 537 sample.vcf
-a---- 2018-06-03 19:41 416 SO_50659915.ps1
> Get-Content .\3fe8e0-421c-4c6a-bfa-38c75df8c07.vcf
BEGIN:VCARD
VERSION:3.0
PRODID:-//Apple Inc.//Mac OS X 10.13.4//EN
N:;;;;
TEL;type=CELL;type=VOICE;type=pref:+18005555555
UID:3fe8e0-421c-4c6a-bfa-38c75df8c07
X-ABUID:3FE8490-421C-4C6A-B2FA-38C15DF8C07:ABPerson
END:VCARD
> Get-Content .\5ad596-a879-4c98-9f56-2ef90efe32.vcf
BEGIN:VCARD
VERSION:3.0
PRODID:-//Apple Inc.//Mac OS X 10.13.4//EN
N:;<blah@company.org>;;;
FN:<blah@company.org>
item1.EMAIL;type=INTERNET;type=pref:blah@company.org
item1.X-ABLabel:_$!<Other>!$_
UID:5ad596-a879-4c98-9f56-2ef90efe32f
X-ABUD:DB5C20C-6DFC-450F-A752-D57964F6F3A:ABPerson
END:VCARD
>
答案 2 :(得分:0)
LotPings打败了我。 无论如何,这是我的解决方案
# Enter the full path and filename of your large combined vcf file here
$InputFile = '<The full path and filename to your vcf file>'
# The path where yhou want the output vcf files. Below defaults to a folder 'VCards' within your Temp directory
$OutputPath = Join-Path $env:TEMP 'VCards'
# Read the input file in a single string
$VCardData = Get-Content $InputFile -Raw
# Create the output folder if it does not already exist
if (!(Test-Path $OutputPath -PathType Container)) {
New-Item -ItemType Directory -Force -Path $OutputPath | Out-Null
}
# Use RegEx match to search for strings across line breaks.
# This regex will keep the "BEGIN:VCARD" and "END:VCARD" for each array element intact
$VcardRegex = '(?s)((?:BEGIN:VCARD).*?(?:END:VCARD))'
# This regex is for parsing out the UID value of the vcard if present
$UidRegex = '\b(?:UID:)(?:urn:)?(?:uuid:)?([0-9a-f\-]*)\b'
# Select all matches
$VCardArray = [RegEx]::Matches($VCardData,$VcardRegex).Value
# Save results to $OutputPath as separate .vcf files
# using the UID value as filename. If no UID is found in the VCard element,
# a safety name is generated using a simple counter $i.
# Each file is encoded in UTF-8 encoding. If you use the Set-Content commandlet with option -Encoding UTF8
# it will create files prefixed with a byte order mark (BOM).
# Because it is usually advisable to create the file without the BOM, i use [System.IO.File]::WriteAllText
# using an encoding object
$Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding $false
$i = 0
$VCardArray | ForEach-Object {
if ($_ -match $UidRegex) {
$fileName = $matches[1] + '.vcf'
}
else {
$fileName = 'Vcard_{0:000}.vcf' -f $i++
}
$fileOut = Join-Path $OutputPath $fileName
try {
[System.IO.File]::WriteAllText($fileOut, $_, $Utf8NoBomEncoding)
Write-Host "Saved file '$fileOut'"
}
catch {
Write-Error "Could not write file '$fileOut':`r`n$($_.Exception.Message)"
}
}