Multi line search and replace
Say I have the following code:
Test.prototype.doSomething = _.debounce( function() {
console.log('hello');
}, 500);
How can I replace the number 500 to 10 in all the files of my project? I can't see to find a way to use regular expression with multi-lines support.
Please sign in to leave a comment.
Hi there,
If you select your text .. and then invoke "Replace in Path" shortcut (Ctrl+Shift+R on Windows/Linux using Default keymap) IDE will be in regex mode straight away with your multi-line text properly escaped:
Thank you Andriy, I wasn't aware of this feature.
However, when I try to use .* to match anything including newlines, it doesn't seem to work. Is there a way for the dot to match newlines as well?
This is the expression I used to match the code I posted above:
And my replace string would be something like:
Accordingly to the devs comments (quite old, actually) -- not possible. The dot (.) symbol will NOT match new lines (\n)
https://youtrack.jetbrains.com/issue/IDEA-69435
In my practice -- such restriction makes sense (otherwise .* would cause too many issues) .. although some check box to lift it up would be good to have for those who needs it.
Thanks for your help, I'll be looking for an alternative then. Too bad the dev team seems to be ignoring this issue :(
Selecting the multi-line text and Ctrl+Shift+R worked like a charm for me on a more simple text phrase.
Find:
Line1
Line2
appeared as:
Line1\n Line2
in the pre-populated window and the replace worked perfectly. Thanks Andriy!
use [\s\S]* instead of .* to consider \n too
text: Some multi- \n line text \n example please
regex : me[\s\S]*?pl
match:
me multi-
line text
exampl
Found this thread while trying to search for my issue but ended up finding it anyway. My use-case is be able to find the @Bean and Generator in the same code block that lives on two separate new lines. For example, being able to find the first to lines in:
In the Find in Path pop up, the regex I use is:
It works like a charm; thank you for the hints.
Thanks Wendigoxs! [\s\S]* is the solution!
I landed here after a search to work out how to make the "." in a regex match newlines.
However then solved my problem using this which will match all chars including newlines:
Thought this was maybe worth recording here.
If you are trying to make the
.
character match newlines (similar toDOTALL
mode in Java) you may find this useful.As the docs say:
This means you can use modifiers such as
(?s)
a the start of the expression which:Mark Crossfield omg thank you so much.