Search & Replace -- Regepx $ not working as expected
Answered
I try to use the IntelliJ editor to do some complex Search&Replace operation. One of them is needed to change every end of the text inside the editor.
Trying to use $ inside a regex doesn't work as expected, no match is found :-(
A corresponding action to change something at the beginning of a line works as expected.
It's the same behavior for Windows and Mac OS with the current IntelliJ version.
Has anyone an explanation why IntelliJ works different the all the other editors I'm using?
Thanks
Peter
Please sign in to leave a comment.
Please clarify what you want to do, you original regex is not visible on the screenshot.
Sorry Serge ...
In the example I tried to replace "End Of Line" by a different Character. The S&R pattern are e.g.
The same for replacing the Begin-Of-Line with ^ instead of $ works as expected.
Can't reproduce in 2022.2.4 or 2022.3 release. Please update.
As I've mentioned I'm using the newest version ...
And it is not working ... : https://www.dropbox.com/s/orydinkn5gxu8pq/Bildschirmaufnahme%202022-12-02%20um%2019.51.46.mov?dl=0
Try with the default settings: File | Manage IDE Settings | Restore Default Settings...: https://i.imgur.com/kbrkC7w.png.
Try File | Invalidate Caches | Invalidate and Restart.
I can replicate the issue in a fresh install of IDEA Community Edition. It doesn't happen for my install of Ultimate, so I dunno what's going on. I uploaded a screen recording to demonstrate: https://youtu.be/pNNSqmflw1w
The workaround is to use groups:
Substitution is then:
Hi Serge,
sorry for the late response. But your "magic" doesn't work, a simple Search&replace of the EOL is still not working.
But I've found out, that "Any Character"+EOL works. In the above example the regex 6$ matches 4 line (which is correct).
Best regards
Peter
What IDE version do you use? There was a bug: https://youtrack.jetbrains.com/issue/IDEA-231072/intellij-idea-incorrect-processing-of-regex-regular-expression-matches-empty-string. It's fixed in the current releases.
IntelliJ IDEA 2022.3 (Ultimate Edition)
Build #IU-223.7571.182, built on November 29, 2022
Licensed to Peter Stöhr
Subscription is active until January 19, 2023.
For educational use only.
Runtime version: 17.0.5+1-b653.14 aarch64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
macOS 13.0.1
GC: G1 Young Generation, G1 Old Generation
Memory: 2048M
Cores: 10
Metal Rendering is ON
Kotlin: 223-1.7.20-release-201-IJ7571.182
Please follow https://youtrack.jetbrains.com/issue/IDEA-307671/EOL-produces-no-matches-occasionally for updates.
WORKAROUND: ensure there is EOL at the end of the file. This can be done by enabling Settings (Preferences on macOS) | Editor | General | Ensure every saved file ends with a line break.
Cool, that really helps!
Thanks!