简体   繁体   中英

Regex excluding text between parentheses in Notepad++ search & replace

Group,

This is my first post. I like to use Notepad++ for cnc program g-code editing. Sometimes a program will contain text with no whitespace between the words. Comments are contained between parentheses. Sometimes lines contain comments, but most times not:

G90G10L2P1X-38.046Y-11.361Z-36.991(G54 B=0)

G90G10L2P1X5.68Y5.69 

G54G00G90X0.0Y-5.0T53

G43H55Z6.0M8 

S300M03

This regex will add a space between all the words, but skips lines that end with a parentheses:

Search: (.)([AZ])(?!.*\\))

Replace: \\1 \\2\\3

So far so good. What I'd like is to add spaces between all words, only excluding the comments, rather than skipping entire lines that contain comments as above.

Any ideas or help will be greatly appreciated! I've been playing with this, and Googling it quite a bit with no success.

** EDITED MY POST TO SHOW MORE COMPLEX EXAMPLE 4/8/15:

(FILE NAME XY9032.010) 
(SUB PR.321.322.323.327.328.329.325.324)
(** NONSENSE COMMENT TEXT 324 TO SHOW A DIFF POSSIBLE COMB AB 2/25/15 **)
N1(STORE TOP BOSS G55.X)
N2(STORE BOT. BOSS G55.X)
N3(STORE BOT. BOSS G55.Y)
G00G17G40G49G80G90 
G91G28X0Y0Z0.M05 
G90G0B0
N4G90G10L2P5X-38.046Y-11.361Z-37.021(G58 B0 BOTTOM BORES)
N5G90G10L2P6X-23.130Y-11.361Z-44.027(G59 B180 BOTTOM BORE) 
G90G10L2P1X#568Y#569 
G54G00G90X0.0Y-5.0T53
G43H55Z6.0M8 
S300M03
Z.6
N6G01Z.015F50. 
G01Y-7.0F9.0 
G02J7.0F18.0 
G00Z6.0M09 
G91G28X0Y0Z0M05
M1 

This small regex will find all instances of text between parentheses:

(\(.+\))

For instance, Search:

(\(.+\))

Replace:

\1****

will add the asterisks after every instance of parentheses in a text file.

I just can't figure out to exclude the same regex expression from a broader search as described elsewhere in this post.

This does not work - still adds spaces within parentheses. underscore=space:

Search: (?!\(.+\))([a-z]-?\d+(?:\.\d+)?)
Replace: $1_

Is this OK for you:

Find what: (?<!\\()([az]-?\\d+(?:\\.\\d+)?)
replace with: $1_
I use _ for visualise a space

With your test case, it gives:

G90 G10 L2 P1 X-38.046 Y-11.361 Z-36.991 (G54 B=0)

G90 G10 L2 P1 X5.68 Y5.69  

G54 G00 G90 X0.0 Y-5.0 T53 

G43 H55 Z6.0 M8  

S300 M03 

If you define a 'word' as a letter followed by numbers, then this regex will capture them:

([a-zA-Z][0-9.-]+)

Then you can replace them by \\1 (backslash one space).

It's difficult to determine what you really consider a "word", but using your examples and comments, I've put together what I think you want. Either way, that's not your real issue. The issue is excluding comments, which this does effectively:

Search: (\\(.*?\\))|([az](?=[-#.\\d])([-#]?\\d*(\\.\\d*)?))

Replace: $1$2_ [underscore for space]

The first part of the alternation, (\\(.*?\\)) , captures any comments, slurping them up so they won't be matched by the second part of the alternation

NOTE: You'll end up with a space at the end of each line, but that's an easy trim on the Notepad++ menu.

Hi i'm a fanuc programer too... i use notepad++ and i come to

(\(.*?\))|([0-9]|(]))([A-Z])

and replace with

\1\2 \4

it's let a space at end but...

and i add (]) to

x#750z0

I come with another option.

Search: ((. ?))|([. ?])|([0-9]|(]))([AZ])|((])(THEN|DO|GOTO))([AZ])

Write: \\1\\2\\3\\4\\6\\7 \\5

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