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

求助:使用Notepad++正则表达式重排G代码文本

Solution for Text Rearrangement in Notepad++ Using Regex

Alright, let's get this text sorted out using Notepad++'s regex feature. I've worked out a solution that fits exactly what you need—here's how to do it step by step:

Step 1: Open the Replace Dialog

Launch Notepad++, open your target text file, then press Ctrl+H to bring up the Replace window. Make sure you select the Regular expression option under "Search Mode", and leave the ". matches newline" box unchecked (we're working with single-line commands here).

Step 2: Use These Regex Patterns

Find What:

(G\d+\s*(?:[XY]\d+(?:\.\d+)?\s*)*) S(\d+)

Replace With:

M42 P11 S$2 $1

Step 3: Run the Replacement

Click Replace All—this will process every matching segment in your text automatically.

How It Works (Breakdown)

Let's break down the regex so you understand why it works for your case:

  • Capture Group 1 ((G\d+\s*(?:[XY]\d+(?:\.\d+)?\s*)*)):
    • G\d+: Matches the G-code prefix (like G0 or G1)
    • \s*: Handles any number of spaces (including zero) between elements
    • (?:[XY]\d+(?:\.\d+)?\s*)*: Matches any number of X/Y parameters, whether they're integers (like Y5) or have decimals (like X28.5). The ?: makes this a non-capturing group so it doesn't interfere with our main captured content.
  • S(\d+): Matches the S value and captures the numeric part (like 0 or 255) as Capture Group 2.

The replacement string swaps the order: we first insert the fixed M42 P11 followed by the captured S value ($2), then add the original G-code and X/Y parameters ($1).

Example Test

For your input text:

G0 X28.5 Y5 S0 G1 X74 S255

After replacement, you'll get exactly what you wanted:

M42 P11 S0 G0 X28.5 Y5 M42 P11 S255 G1 X74

Bonus: Handle Decimal S Values

If your S values ever include decimals (like S123.45), just tweak the "Find What" regex to:

(G\d+\s*(?:[XY]\d+(?:\.\d+)?\s*)*) S(\d+(?:\.\d+)?)

This will capture both integer and decimal S values correctly.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 08:48:29