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

如何用Notepad++正则表达式批量反转文本语序?附示例

实现文本语序反转(支持多文本行)

方法一:Notepad++ 正则表达式实现

Notepad++ 可通过多次正则替换实现行内单词反转,步骤如下:

  1. 打开目标文本,按下 Ctrl+H 打开替换窗口
  2. 切换到「正则表达式」模式,取消勾选「.匹配换行符」
  3. 「查找内容」框输入:(\S+)(\s+)(\S+)
  4. 「替换为」框输入:$3$2$1
  5. 反复点击「全部替换」,直到软件提示「0 处替换完成」——这一步会逐次交换行内相邻单词,最终实现整行单词顺序反转
  6. 最后用一次替换清理行尾多余空格:查找 \s+$,替换为空

如果追求高效,可安装Notepad++的「Python Script」插件,运行以下脚本一次性处理所有行:

editor.beginUndoAction()
line_count = editor.getLineCount()
for i in range(line_count):
    line = editor.getLine(i).strip()
    reversed_line = ' '.join(line.split()[::-1])
    editor.replaceLine(i, reversed_line)
editor.endUndoAction()

方法二:命令行工具(awk)

用awk可快速批量处理文本,终端执行以下命令(假设目标文件为input.txt,输出到output.txt):

awk '{for(i=NF;i>0;i--) printf "%s ",$i; print ""}' input.txt > output.txt

该命令逐行读取文本,反转每行单词顺序后输出。

方法三:Python脚本

若熟悉Python,可编写简单脚本处理任意数量文本行:

with open('input.txt', 'r', encoding='utf-8') as f, open('output.txt', 'w', encoding='utf-8') as out_f:
    for line in f:
        words = line.strip().split()
        reversed_words = words[::-1]
        out_f.write(' '.join(reversed_words) + '\n')

运行脚本后,output.txt将保存所有行反转后的结果。

内容的提问来源于stack exchange,提问作者omid leon

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 15:12:05