You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:PowerShell中合并多行章节标题并标准化的方法

Standardizing Chapter Titles in PowerShell for Memoir Text Files

Hey Steve, let's get those chapter titles standardized properly for your family's memoirs—this is such a meaningful project to work on! The issue with your initial code is that it doesn't handle multi-line whitespace or use regex to flexibly match messy formatting. Here's a robust solution using PowerShell's regex capabilities to fix all those edge cases:

Step 1: Test with a Sample Messy Title

First, let's verify the logic works with your problematic inputs. Run this snippet to see how it cleans up both single-line and multi-line titles:

# Sample messy input (includes leading/trailing spaces and line breaks)
$messyTitle = @"
 chapter three 
Chapter 1 text text text. chapter two text text
"@

# Normalize all chapter titles
$cleanText = [regex]::Replace($messyTitle, '(?i)\s*(chapter\s+.+?)\s*', {
    param($match)
    # Trim whitespace and convert the entire title to uppercase
    $match.Groups[1].Value.Trim().ToUpper()
})

# Output the cleaned result
Write-Host $cleanText

Output:

CHAPTER THREE
CHAPTER 1 TEXT TEXT TEXT. CHAPTER TWO TEXT TEXT

Step 2: Batch Process All Your Memoir Files

To apply this to all your yearly text files, use this script. It reads each file as a single string (preserving line breaks), cleans up every chapter title, and saves the standardized content back to the file:

# Replace this with the path to your folder of memoir text files
$memoirFolder = "C:\Your\Memoir\File\Path"

# Get all .txt files in the folder
$textFiles = Get-ChildItem -Path $memoirFolder -Filter *.txt

# Loop through each file to clean titles
foreach ($file in $textFiles) {
    # Read the entire file as one string (critical for handling multi-line titles)
    $fileContent = Get-Content -Path $file.FullName -Raw

    # Use regex to standardize chapter titles:
    # - (?i): Ignore case to match "chapter", "Chapter", etc.
    # - \s*: Match any leading/trailing whitespace (including line breaks)
    # - (chapter\s+.+?): Capture "chapter" plus the title content (non-greedy to stop at the end of the title)
    # - (?=\.|\s|$): Stop matching at a period, whitespace, or end of line to avoid over-matching
    $cleanContent = [regex]::Replace($fileContent, '(?i)\s*(chapter\s+.+?)(?=\.|\s|$)', {
        param($match)
        $match.Groups[1].Value.Trim().ToUpper()
    })

    # Save the cleaned content back to the file (always back up originals first!)
    $cleanContent | Set-Content -Path $file.FullName -Encoding UTF8
    Write-Host "Successfully processed: $($file.Name)"
}

Key Notes to Avoid Issues

  • Backup First: Always make a copy of your original files before running batch scripts—better safe than sorry!
  • Test with One File: Run the script on a single test file first to confirm it works as expected for your specific title formatting.
  • Adjust the Regex if Needed: If your titles include special characters (like quotes) or have unique formatting, tweak the regex pattern to match those cases. For example, if titles end with exclamation points, add ! to the stop condition: (?=\.|\s|!|$).

内容的提问来源于stack exchange,提问作者Steve A

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 06:49:27