Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.4k views
in Technique[技术] by (71.8m points)

regex - Difficulties with adding spaces around equal signs using regular expressions with Notepad++

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

and I want to change it to:

foo = 4
bar[index] = 4

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++ 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)

in "find what" together with:

1 = 2

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.

Edit: 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 .

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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)

Is find any = character that

  • is preceded by one of w, ] or ), as in (?<=[w])]), and
  • is followed by a w, as in (?=w).

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)

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...