简体   繁体   中英

Powershell escape single quotes

I'm try to write a script to go through some file but it keep remove any words after '

$regex = "\[([\w|\s*-]+)\]([\w|\s]+(?:\[[\w|\s]+\])?.*)\s*-\s*([\w|,|\s.]+)";
Get-ChildItem -LiteralPath $p -Filter *.txt|
    ForEach-Object {
        $name = $_.BaseName
        $groups = [regex]::Match($name, $regex).Captures.Groups
        write-host $groups[1] $groups[2] $groups[3]
}

Format for file name [Test] Test - Test'Test

The out put comes as [Test] Test - Test it keep missing any single quotes, how do i escape this

how do it get [Test] Test - Test'Test

You don't include a single quote as a valid character in the last grouping ([\\w|,|\\s.]+) . Just add a single quote there to include that:

$regex = "\[([\w\s*-]+)\]([\w\s]+(?:\[[\w\s]+\])?.*)\s*-\s*([\w,\s.']+)"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM