bat批处理的代码如下
@echo offsetlocal enabledelayedexpansion:: 设置文件夹路径set "folderPath=D:\txt\":: 遍历文件夹里的所有 txt 文件for %%F in (%folderPath%\*.txt) do ( set "inputFile=%%F" set "outputFile=%%F_temp.txt" :: 第一步:替换特定字符为空 powershell -Command "(Get-Content '!inputFile!') -replace '[「」【】]', '' | Set-Content '!outputFile!'" :: 第二步:用正则表达式限制每行的字符数并添加新行 powershell -Command "(Get-Content '!outputFile!') -replace '(.{6,18}[,。:!?]|.{16})', '$1`n' | Set-Content '!outputFile!'" :: 第三步:替换特定字符为空 powershell -Command "(Get-Content '!outputFile!') -replace '[,。!]', '' | Set-Content '!outputFile!'" :: 第四步:移除空行 powershell -Command "(Get-Content '!outputFile!') | ? {$_ -ne ''} | Set-Content '!outputFile!'" :: 替换原文件 move /Y "!outputFile!" "!inputFile!" echo 文件 %%F 处理完成!)echo 所有文件处理完成!pause
其中第二步中的用正则表达式限制每行的字符数并添加新行
powershell -Command "(Get-Content '!outputFile!') -replace '(.{6,18}[,。:!?]|.{16})', '$1`n' | Set-Content '!outputFile!'"
可以实现正则定位,但是无法实现换行,要如何修改,\n \r 设置了无效?
$1 部分正常识别,但是一到换行就直接输出 n 并不能换行
@echo off
setlocal enabledelayedexpansion
:: 设置文件夹路径
set "folderPath=D:\txt\"
:: 遍历文件夹里的所有 txt 文件
for %%F in (%folderPath%*.txt) do (
set "inputFile=%%F"set "outputFile=%%F_temp.txt":: 第一步:替换特定字符为空powershell -Command "(Get-Content '!inputFile!') -replace '[「」【】]', '' | Set-Content '!outputFile!'":: 第二步:用正则表达式限制每行的字符数并添加新行powershell -Command "(Get-Content '!outputFile!') -replace '(.{6,18}[,。:!?]|.{16})', '$1^' | Set-Content '!outputFile!'":: 第三步:替换特定字符为空powershell -Command "(Get-Content '!outputFile!') -replace '[,。!]', '' | Set-Content '!outputFile!'":: 第四步:移除空行powershell -Command "(Get-Content '!outputFile!') | ? {$_ -ne ''} | Set-Content '!outputFile!'":: 替换原文件move /Y "!outputFile!" "!inputFile!"echo 文件 %%F 处理完成!
)
echo 所有文件处理完成!
pause
在第二步中,我们将$1^用于添加换行符,其中$1表示正则表达式匹配到的内容,而^表示换行。这样,每次匹配到字符数限制或特定字符后,都会在匹配的位置添加换行符.
试试这个?
用python怎么样
replace.py
import reimport osimport sysdef handle(folder_path): if not os.path.exists(folder_path): print("{} 为无效路径".format(folder_path)) return # 按顺序执行正则替换,可自行添加正则 pattern_replacements = [[r'[「」【】]',''], [r'(.{6,18}[,。:!?]|.{16})',r'\1\r\n'], [r'[,。!]',''], [r'[\r\n]+','\n']] data = '' for file in os.listdir(folder_path): file_path = os.path.join(folder_path,file) if not os.path.isdir(file_path) and file_path.endswith('.txt'): with open(file_path,'r',encoding='utf-8') as fr: data = fr.read() for pattern,replacement in pattern_replacements: data = re.sub(pattern,replacement,data) with open(file_path,'w',encoding='utf-8') as fw: fw.write(data) print("{} 处理完成".format(file)) print("全部处理完成") if __name__ == "__main__": folder_path = r"d:\txt" if len(sys.argv) > 1: folder_path = sys.argv[1] handle(folder_path)
start.bat
@echo offset "folderPath=D:\txt\"REM 检查系统环境变量中是否存在Python路径where python > nul 2>&1if %errorlevel% equ 0 ( REM Python路径已添加到系统环境变量中 set "python_path=python") else ( REM Python路径未添加到系统环境变量中,需手动设置Python路径,以下为测试路径 set "python_path=D:\Env\Python\Python38\python.exe")%python_path% replace.py %folderPath% || pausepause
把start.bat和replace.py放到同一个文件夹,配置下python路径就可以执行批处理文件了
@echo off setlocal enabledelayedexpansion set "outputFile=C:\path\to\your\file.txt" for /f "delims=" %%a in ('type "%outputFile%"') do ( set "line=%%a" set "newLine=!line:~,18!" echo !newLine!>> "%outputFile%" )
为什么不是\n
问题内容: 我有一张约有50万行的表格;varchar(255)UTF8列包含一个文件名; 我正在尝试从文件名中删除各种奇怪的字符-以为我会使用字符类: 现在, MySQL中是否有一个函数可以让您通过正则表达式进行替换 ?我正在寻找与REPLACE()函数类似的功能-简化示例如下: 我知道REGEXP / RLIKE,但那些只检查 是否 有匹配,没有 什么 比赛是。 (我 可以 做一个“ ”从PH
我想以firstName和lastname的形式获得输出。怎么用正则表达式做,有什么想法吗?
问题内容: 我目前正在将Web应用程序代码从PHP移植到JS。 我对此正则表达式有疑问。从PHP 这样使用 如何将其转换为在JS上工作? 先感谢您 问题答案: 没什么特别的。PHP regex语法与JavaScript中的语法非常相似: 您可以从 MDN的 本手册中找到有关JavaScript 中正 则表达式的更多信息:https : //developer.mozilla.org/en- US/
问题内容: 谁能告诉我如何从PHP脚本执行.bat文件? 我努力了: 什么都没用。我检查了PHP手册并在Google周围搜索,但找不到一个好的答案。有人知道我要去哪里错吗? 我正在运行Windows 2003 Server,并且已成功手动运行.bat文件,它可以执行我需要的操作;我只需要能够以编程方式启动它。 问题答案: 您可能需要通过来运行它,例如:
问题内容: 我有一个字符串,在和之间包含普通字符,白色字符集和换行符。此正则表达式不起作用:。这是因为与换行符不匹配。我的问题是,该怎么做? 问题答案: 您需要使用DOTALL修饰符。 这可能无法完全满足您的需求,因为您的贪婪匹配。您可以改为尝试非贪婪匹配: 如果没有其他标签,您也可以通过匹配除“ <”以外的所有内容来解决此问题: 另一个观察结果是,您不需要将其用作正则表达式定界符。使用另一个字符
Parameter Position参数位置 Type类型 Required必需 Default默认 Description 1 string Yes n/a This is the regular expression to be replaced. 替换正则表达式. 2 string Yes n/a This is the string of text to replace with. 用来替