简体   繁体   English

正则表达式排除记事本中的括号之间的文本

[英]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. 我喜欢使用Notepad ++进行cnc程序g代码编辑。 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])(?!.*\\)) 搜索: (.)([AZ])(?!.*\\))

Replace: \\1 \\2\\3 替换: \\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: **编辑我的文章以显示更多复杂的示例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. 我只是想不出从本文其他地方描述的更广泛的搜索中排除相同的regex表达式。

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+)?) 查找内容: (?<!\\()([az]-?\\d+(?:\\.\\d+)?)
replace with: $1_ 替换为: $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). 然后,您可以将它们替换为\\1 (反斜杠一个空格)。

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*)?)) 搜索: (\\(.*?\\))|([az](?=[-#.\\d])([-#]?\\d*(\\.\\d*)?))

Replace: $1$2_ [underscore for space] 替换: $1$2_ [下划线表示空格]

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. 注意:您最终将在每行的末尾都有一个空格,但这是在Notepad ++菜单上的简单修饰。

Hi i'm a fanuc programer too... i use notepad++ and i come to 嗨,我也是fanuc程序员...我使用notepad++ ,我来

(\(.*?\))|([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]) 搜索:((。 ?))|([。 ?])|([0-9] |([]))([AZ])|(([](Then | DO | GOTO))([AZ])

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM