3

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.

Community
  • 1
  • 1
magnetometer
  • 646
  • 4
  • 12
  • "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." Well, considering that it has a dedicated "replace all" button in the first place, I'm pretty sure that is the intended behavior. – BoltClock Aug 27 '14 at 12:27
  • I agree that this is the intended behaviour of the "replace all" button. But I would expect the "replace" button to replace a single occurence, which doesn't work for find `(?<=[\w\]\)])=(?=\w)`, replace with ` = ` , but works for find `([\w\]\)])=(\w)`, replace with `\1 = \2` . – magnetometer Aug 27 '14 at 12:33
  • Ah, I misread. OK, I just tested this out for myself, and what I'm seeing is that Notepad++ finds the text and is able to step through several matches correctly, but doesn't actually replace anything. I'm not sure then - it might be a bug. – BoltClock Aug 27 '14 at 12:36

4 Answers4

2

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.

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
  • Thank you for your detailed explanation. I edited my question to provide more detail and to clarify my use of the "replace with" field. My problem seems to be caused by an unexpected difference in behaviour of Notepad++ – magnetometer Aug 27 '14 at 12:26
  • I've accepted your answer, as it gives the most detailed explanation for the regular expressions in question, even though the original problem seems to be caused by Notepad++ showing a different behaviour for "replace" and "replace all". – magnetometer Aug 28 '14 at 07:35
0

With your initial regex,

Regex:

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

REplacement string:

 =<space>

I added <space> instead of . Lookarounds would do a zero width match. It won't match any characters.

DEMO

Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
0
  (?<=[\w\]\)])(=)(?=\w)

You can use this . Replacement by

 " \1 "

See Demo.

http://regex101.com/r/vY0rD6/2

Your earlier regex had no captured pattern.So just added that.

vks
  • 67,027
  • 10
  • 91
  • 124
0

In your first Regular Expression(?<=[\w\]\)])=(?=\w) there's no capturing groups so you can't use back references (replacing with \1 = \2)

But you can use space=space as replacement with the above regex where space is a real white space character.

revo
  • 47,783
  • 14
  • 74
  • 117