2017-01-10 2 views
0
subdomain1.google.nl 
subdomain1.subdomain2.google.com 
subdomain1.facebook.com 
subdomain1.subdomain2.facebook.com 

Wie kann ich (von Powershell) entfernen Sie die Topleveldomain (TLD), so wird meine Ergebnisse sein:Entfernen Sie alles nach. (Punkt). Einschließlich Punkt

subdomain1.google 
subdomain1.subdomain2.google 
subdomain1.facebook 
subdomain1.subdomain2.facebook 
+2

' 'subdomain1.subdomain2.facebook.com' -replace '\. [^.] *? $'' –

Antwort

1

Sie Katze regex Muster versuchen Oder Sie konnten dieses versuchen:

$string = 'subdomain1.google.nl' 
[io.path]::GetFileNameWithoutExtension($string) 

Ausgabe:

subdomain1.google 

Eigentlich! Ich war so gespannt, was die beste Perfomance weise Methode wäre, und erraten, was:

$results = @() 
for ($i=1;$i -le 1000;$i++){ 
    $obj = New-Object PSObject 
    $obj | Add-Member -MemberType NoteProperty -Name 'Execution #' -Value $([int]$i) 
    $obj | Add-Member -MemberType NoteProperty -Name 'IOPath_Ticks' -Value $(Measure-Command -Expression {[System.IO.Path]::GetFileNameWithoutExtension('subdomain1.google.nl')} | Select -ExpandProperty Ticks) 
    $obj | Add-Member -MemberType NoteProperty -Name 'Regex_Ticks' -Value $(Measure-Command -Expression {'subdomain1.google.nl' -replace '\.[^.]*?$'} | Select -ExpandProperty Ticks) 
    $results += $obj 
} 

Durchschnittliche

$results | Measure-Object -Property IOPath_Ticks -Average 

Count : 1000 
Average : 129,978 
Sum  : 
Maximum : 
Minimum : 
Property : IOPath_Ticks 

$results | Measure-Object -Property Regex_Ticks -Average 
Count : 1000 
Average : 364,56 
Sum  : 
Maximum : 
Minimum : 
Property : Regex_Ticks 

Summe

$results | Measure-Object -Property IOPath_Ticks -Sum 
Count : 1000 
Average : 
Sum  : 129978 
Maximum : 
Minimum : 
Property : IOPath_Ticks 

$results | Measure-Object -Property Regex_Ticks -Sum 
Count : 1000 
Average : 
Sum  : 364560 
Maximum : 
Minimum : 
Property : Regex_Ticks 

So, wie Sie sehen können, wenn Sie brauchen, um eine große Anzahl von Datensätzen verarbeiten Verwendung von .NET-Klassen würde am besten im Vergleich zu Regexen

+0

Kreative Missbrauch von statischen Methoden FTW. ;) –

+0

@AnsgarWiechers, wahr! Ich habe ein paar Tests, schau dir die Zahlen an :-) –

0

Versuchen Sie als:

$val="subdomain1.google.nl" 
$val.substring(0,$val.LastIndexOfAny(".")) 
$val="subdomain1.subdomain2.google.com" 
$val.substring(0,$val.LastIndexOfAny(".")) 
$val="subdomain1.facebook.com" 
$val.substring(0,$val.LastIndexOfAny(".")) 
$val="subdomain1.subdomain2.facebook.com" 
$val.substring(0,$val.LastIndexOfAny("."))