簡體   English   中英

使用powershell捕獲數組中的正則表達式匹配

[英]Trapping regex matches in an array with powershell

我們有一個從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]
    }
}

總是引用你的來源

但是當我把我的正則表達式放到這個代碼中時,它找不到任何匹配項

您只需在您發布的每個代碼塊中要求一個匹配項。 您想要使用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

此PowerShell腳本

  • 使用非消耗的RegEx將輸入拆分為以BEGIN:VCARD開頭的塊。
  • 它檢查內部是否有UID並相應地命名輸出文件
  • 所有其他都被命名為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

>

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)"
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM