删除所有不以字母或标点符号开头的行

时间:2015-08-23 19:12:42

标签: regex replace sublimetext3

我有一个很长的文本文件,格式如下:

1
00:00:00,000 --> 00:00:16,700
to use 2 languages.

2
00:00:16,700 --> 00:00:19,600
I was saying that we are going to use 2 languages

3
00:00:19,600 --> 00:00:24,700
...I myself will continue to speak because of time

现在我想要删除所有文本,所以结果应该是:

to use 2 languages.
I was saying that we are going to use 2 languages
...I myself will continue to speak because of time

正确的正则表达式命令是什么?还会删除包含数字的所有行的命令。我使用的是Sublime Text或regex101.com

2 个答案:

答案 0 :(得分:2)

TypeName: System.Management.Automation.PSCustomObject

Name             MemberType   Definition                     
----             ----------   ----------                     
Equals           Method       bool Equals(System.Object obj) 
GetHashCode      Method       int GetHashCode()              
GetType          Method       type GetType()                 
ToString         Method       string ToString()              
CDROM_Connected  NoteProperty object CDROM_Connected=null    
CDROM_ISO        NoteProperty object CDROM_ISO=null          
Floppy_Connected NoteProperty object Floppy_Connected=null   
Floppy_ISO       NoteProperty object Floppy_ISO=null         
GuestType        NoteProperty string GuestType=vmkernel6Guest
IPAddress        NoteProperty string IPAddress=192.168.199.40
ServerName       NoteProperty string ServerName=ESX-VSAN3    
vCPU             NoteProperty string vCPU=2                  
vHDD             NoteProperty Object[] vHDD=System.Object[]  
vNIC             NoteProperty Object[] vNIC=System.Object[]  
vRAM             NoteProperty string vRAM=5120   


function Get-ServerFromXML
{
 <##>

[CmdletBinding()]
param
(
    [String]
    $XMLFile,

    [String]
    $GuestName
)
$xml = New-Object -TypeName XML
$xml.Load($XMLFile)
$Server = Select-Xml -Xml $xml -XPath ('/Servers/Server[@Name = "' + $GuestName + '" ]')
$vNicObjs = @()
$vHddobjs = @()
If ($Server.Node.Hardware.vnic.Count -ne $null)
{
    $Server.Node.Hardware.vnic | %{
        $NicProps = @{
            NIC_ID = $_.ID;
            NIC_Type = $_.Type;
            NIC_StartConnected = $_.StartConnected;
            NIC_MACAddress = $_.MACAddress;
            NIC_WakeOnLan = $_.WakeOnLan;
            NIC_PortGroup = $_.PortGroup
        }
        $vNicObj = New-Object -TypeName System.Management.Automation.PSObject -Property $NicProps
        $vNicObjs += $vNicObj
    }
}
Else
{
    $NicProps = @{
        NIC_ID = $Server.Node.Hardware.vnic.ID;
        NIC_Type = $Server.Node.Hardware.vnic.Type;
        NIC_StartConnected = $Server.Node.Hardware.vnic.StartConnected;
        NIC_MACAddress = $Server.Node.Hardware.vnic.MACAddress;
        NIC_WakeOnLan = $Server.Node.Hardware.vnic.WakeOnLan;
        NIC_PortGroup = $Server.Node.Hardware.vnic.PortGroup
    }
        $vNivObj = New-Object -TypeName System.Management.Automation.PSObject -Property $NicProps
        $vNicObjs += $vNivObj
}

If ($Server.Node.Hardware.vHDD.Count -ne $null)
{
    $Server.Node.Hardware.vHDD | %{
        $HDDProps = @{
            HDD_ID = $_.ID;
            HDD_Type = $_.Type;
            HDD_Size = $Server.Node.Hardware.vHDD.Size;
            HDD_Datastore = $_.Datastore;
            HDD_StorageFormate = $_.StorageFormat
        }
        $vHDDObj = New-Object -TypeName System.Management.Automation.PSObject -Property $HDDProps
        $vHDDObjs += $vHDDObj
    }
}
Else
{
    $HDDProps = @{
        HDD_ID = $Server.Node.Hardware.vHDD.ID;
        HDD_Type = $Server.Node.Hardware.vHDD.Type;
        HDD_Size = $Server.Node.Hardware.vHDD.Size;
        HDD_DataStore = $Server.Node.Hardware.vHDD.Datastore;
        HDD_StorageFormat = $Server.Node.Hardware.vHDD.StorageFormat
    }
    $vHDDObj = New-Object -TypeName System.Management.Automation.PSObject -Property $HDDProps
    $vHDDObjs += $vHDDObj
}


$ServerProps =[Ordered]@{
    ServerName = $Server.Node.Description.Name;
    GuestType = $Server.Node.Description.GuestType;
    IPAddress = $Server.Node.Description.PrimaryIP;
    vCPU = $Server.Node.Hardware.vCPU;
    vRAM = $Server.Node.Hardware.vRAM;
    vHDD = $vHddobjs;
    vNIC = $vNicObjs;
    CDROM_ISO = $Server.Node.Hardware.CDROM.ISOPath
    CDROM_Connected = $Server.Node.Hardware.CDROM.StartConnected
    Floppy_ISO = $Server.Node.Hardware.Floppy.ISOPath
    Floppy_Connected = $Server.Node.Hardware.Floppy.StartConnected
}

$ServerObj = New-Object -TypeName System.Management.Automation.PSObject -Property $ServerProps
$ServerObj
}

function New-BaseGuest
{
 <##>

[CmdletBinding(PositionalBinding = $true,
               SupportsShouldProcess = $true)]
param
(
    [Parameter(Mandatory = $true,
               ValueFromPipeline = $false,
               ValueFromPipelineByPropertyName = $true,
               Position = 1)]
    [string]
    $ServerName,

    [Parameter(Mandatory = $true,
               ValueFromPipeline = $true,
               ValueFromPipelineByPropertyName = $true,
               Position = 2)]
    [string]
    $GuestType,

    [Parameter(Mandatory = $true,
               ValueFromPipeline = $true,
               ValueFromPipelineByPropertyName = $true,
               Position = 3)]
    [string]
    $vCPU,

    [Parameter(Mandatory = $true,
               ValueFromPipeline = $true,
               ValueFromPipelineByPropertyName = $true,
               Position = 4)]
    [string]
    $vRAM,

    [Parameter(Mandatory = $true,
               ValueFromPipelineByPropertyName = $true,
               Position = 5)]
    [system.object]
    $vHDD,

    [Parameter(Mandatory = $true,
               ValueFromPipelineByPropertyName = $true,
               Position = 6)]
    [system.object]
    $vNIC,

    [Parameter(Mandatory = $false,
               ValueFromPipeline = $true,
               ValueFromPipelineByPropertyName = $true,
               Position = 7)]
    [string]
    $CDROM_ISO = $Null,

    [Parameter(Mandatory = $false,
               ValueFromPipeline = $true,
               ValueFromPipelineByPropertyName = $true,
               Position = 8)]
    [string]
    $FLOPPY_ISO = $Null,

    [Parameter(Mandatory = $false,
               ValueFromPipeline = $true,
               ValueFromPipelineByPropertyName = $true,
               Position = 9)]
    [string]
    $CDROM_Connected = $Null,

    [Parameter(Mandatory = $true,
               ValueFromPipeline = $true,
               ValueFromPipelineByPropertyName = $true,
               Position = 10)]
    [string]
    $Floppy_Connected = $Null
)

BEGIN 
{
    $Datastore  = ($vHDD | Where-Object { $_.HDD_ID -eq 1 } | Select-Object -Property HDD_DataStore).HDD_DataStore
    $PortGroup  = ($vNIC | Where-Object { $_.NIC_ID -eq 1 } | Select-Object -Property NIC_PortGroup).NIC_PortGroup
    $DiskSize   = ($vHDD | Where-Object { $_.HDD_ID -eq 1 } | Select-Object -Property HDD_Size).HDD_Size
    $DiskFormat = ($vHDD | Where-Object { $_.HDD_ID -eq 1 } | Select-Object -Property HDD_StorageFormat).HDD_StorageFormat
}
PROCESS
{
    New-VM -Name $ServerName `
           -Datastore $Datastore `
           -NumCPU $vCPU `
           -MemoryGB $vRAM`
           -DiskGB $DiskSize `
           -NetworkName $PortGroup `
           -DiskStorageFormat $CDROM_ISO `
           -GuestID $GuestType | Out-Null
}
END
{

}
}

这似乎是一个非常好的RegEx。用/(?:^|\n)\d+\n[\d\:\,\s\->]+/g 代替,而你只剩下单词。

Demo

答案 1 :(得分:1)

这些都需要多行不区分大小写的模式。
它们在正则表达式中排列,但可以指定为查找选项之一。

替换是一个空字符串。

对于标点符号,它使用属性:

 # (?im)(?:^[^\p{punct}a-z].*\s*)+

 (?im)
 (?:
      ^ 
      [^\p{punct}a-z] 
      .* 
      \s* 
 )+

这个使用POSIX:

 # (?im)(?:^[^[:punct:]a-z].*\s*)+

 (?im)
 (?:
      ^ 
      [^[:punct:]a-z] 
      .* 
      \s* 
 )+