在 Notepad++ 中使用正则表达式在等号周围添加空格的困难 [英] Difficulties with adding spaces around equal signs using regular expressions with Notepad++

查看:93
本文介绍了在 Notepad++ 中使用正则表达式在等号周围添加空格的困难的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试通过添加以下内容来提高我的一些 Python 脚本的可读性等号周围的空格.例如,当前的作业如下所示:

I am currently trying to improve the readability of some of my python scripts by adding spaces around the equal signs. For example, currently an assignment looks like this:

foo=4
bar[index]=4

我想把它改成:

foo = 4
bar[index] = 4

我已经找到以下已回答的问题 Notepad++:使用正则表达式搜索和替换,这表明在查找内容"中使用以下正则表达式应该可行:

I've already found the following answered question Notepad++: Search and Replace with Regular Expression which suggests that using the following regex in "find what" should work:

(?<=[\w\]\)])=(?=\w)

Notepad++ 正确地找到了所有对应的等号,但它不会替换它们,无论我尝试用什么替换它们.我现在正在使用:

Notepad++ correctly finds all corresponding equal signs, but it doesn't replace them, no matter what I try to replace them with. I am now using:

([\w\]\)])=(\w)

在查找内容"中与:

\1 = \2

在替换为"中,它完成了这项工作.但是,我不明白为什么初始正则表达式不起作用,特别是因为它(好吧,相当于它的东西)在链接的问题中被标记为正确.它不适用于 Notepad++ 6.6.1 或 6.6.8.我对正则表达式不是很熟悉,这是我第一次在 Notepad++ 中使用它们,所以我很感激任何帮助.

in "replace with", which does the job. However, I don't understand why the initial regex doesn't work, especially as it (well, something equivalent to it) is marked as correct in the linked question. It doesn't work in either Notepad++ 6.6.1 or 6.6.8. I am not very familiar with regular expressions and this is the first time that I am using them in Notepad++, so I would appreciate any help.

澄清一下:在我的任何尝试中,我都没有将替换为"字段留空,而是总是用 = 或其他字符串填充它.对于我最初的正则表达式,我没有使用 \1 = \2 .

To clarify: I didn't leave the "replace with" field empty in any of my attempts, but always filled it with something, either with = or some other string. For my initial regex, I didn't use \1 = \2 .

但我想我已经确定了问题所在.到目前为止,我已经尝试了所有建议,但似乎没有一个奏效.但是一旦我点击全部替换"而不是替换",Notepad++ 确实正确替换了所有内容,即使是我最初的正则表达式.我不确定这是否是预期的行为.

But I think I have identified the problem. I've tried all the suggestions so far, but none of them seemed to work. But as soon as I clicked on "replace all" instead of "replace", Notepad++ did replace everything correctly, even with my initial regex. I am not sure if this is the intended behaviour.

推荐答案

该问题的已接受答案仅包含搜索表达式.提问者留下评论指出替换字符串应留空.这样做是删除任何匹配的内容.

The accepted answer to that question only contains the search expression. The question asker left a comment pointing out that the replacement string should be left empty. What that does is delete whatever is matched.

您在这里要做的不是完全删除 = 操作符(因为这会非常灾难性地破坏您的脚本!),而只是用空格填充它们.

What you're looking to do here is not to delete the = operators outright (as that would break your scripts quite catastrophically!), but simply to space-pad them.

你原来的正则表达式做了什么:

What your original regex does:

(?<=[\w\]\)])=(?=\w)

是否找到任何 = 字符

  • 前面是 \w]) 之一,如 (?<=[\w\]\)]),和
  • 后跟一个 \w,如 (?=\w).
  • is preceded by one of \w, ] or ), as in (?<=[\w\]\)]), and
  • is followed by a \w, as in (?=\w).

(?<=)(?=) 部分是 后视断言和前瞻断言,而不是捕获组一>.由于您的原始正则表达式不捕获任何内容,因此替换为 \1 = \2 将无法按预期工作.

The (?<=) and (?=) portions are lookbehind and lookahead assertions respectively, not capture groups. Since your original regex doesn't capture anything, replacing with \1 = \2 won't work as expected.

在您的工作正则表达式中:

In your working regex:

([\w\]\)])=(\w)

?<=?= 标记被删除,这使得两个括号捕获组.这允许 \1 = \2 反向引用工作,在第一个捕获、= 符号和第二个捕获之间适当地插入空格.

The ?<= and ?= tokens are removed which makes the two parentheticals capture groups. This allows the \1 = \2 backreferences to work, inserting spaces between the first capture, the = sign, and the second capture appropriately.

附带说明(因为您已经找到了一个可行的解决方案),您仍然可以使原始正则表达式正常工作,但替换字符串应该只是 = 用空格包围.由lookbehind和lookahead断言测试的东西从来没有真正被选中——唯一匹配的字符是=符号本身——所以当你替换时它不会消失.

On a side note (since you've already found a working solution), you can still make the original regex work, but the replacement string should simply be = surrounded by spaces. The stuff that is tested by the lookbehind and lookahead assertions is never actually picked up — the only character that is matched is the = sign itself — so it will not disappear when you replace.

这篇关于在 Notepad++ 中使用正则表达式在等号周围添加空格的困难的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆