Difficulty 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 equal signs. For example, currently the assignment looks like this:
foo=4
bar[index]=4
and I want to change it to:
foo = 4
bar[index] = 4
I already found the following answer to the question "Notepad ++: Search and Replace with Regular Expression" which says that using the following regex in "find what" should work:
(?<=[\w\]\)])=(?=\w)
Notepad ++ finds all matching equal signs correctly, but does not replace them, no matter what I try to replace them. Now I am using:
([\w\]\)])=(\w)
in "find what" along with:
\1 = \2
in "replace with", which does the job. However, I don't understand why the original regex is not working, especially since 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 I have used them in Notepad ++, so I would appreciate any help.
Edit: To clarify: I did not leave the "replace to" field empty in any of my attempts, but always filled it with something, either with a help =
or some other string. For my original regex, I didn't use \1 = \2
.
But I think I have identified the problem. So far I've tried all the suggestions, but none of them seemed to work. But once I hit "replace all" instead of "replace", Notepad ++ did replace everything correctly, even with my initial regex. I'm not sure if this is the intended behavior.
source to share
The accepted answer to this question only contains the search expression. The respondent left a comment indicating that the replacement string should be empty. What does it mean to delete everything that matches.
What you want to do is not just remove the statements =
(as this can be disastrous for your scripts!), But simply to place them in space.
What your original regex does:
(?<=[\w\]\)])=(?=\w)
Find any character =
that
- preceded by one of
\w
,]
or)
, as in(?<=[\w\]\)])
, and for - follows
\w
, as in(?=\w)
.
(?<=)
and (?=)
are lookbehind and lookahead assertions , not capture groups . Since your original regex doesn't grab anything, substitution with \1 = \2
will not work as expected.
In your working regex:
([\w\]\)])=(\w)
Agents ?<=
and ?=
are removed, which makes two groups of entries in parentheses. This allows you to work with backreferences by \1 = \2
inserting spaces between the first capture, sign, =
and second capture as appropriate.
On the side of the note (since you've already found a working solution), you can still do the original regex, but the replacement string must be =
surrounded by spaces. The stuff checked by the lookbehind and lookahead assertions is never matched - the only character that matches is the token =
, so it won't go away when replaced.
source to share
(?<=[\w\]\)])(=)(?=\w)
You can use this. Replacement
" \1 "
See demo.
http://regex101.com/r/vY0rD6/2
There was no fixed pattern in your previous regex. Just added that.
source to share