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.

9 comments
Comment actions Permalink

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:

2
Comment actions Permalink

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:

(Test\.prototype\.doSomething \= .*) (500)(\)\;)

And my replace string would be something like:

$1 10$3
1
Comment actions Permalink

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.

3
Comment actions Permalink

Thanks for your help, I'll be looking for an alternative then. Too bad the dev team seems to be ignoring this issue :(

0
Comment actions Permalink

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!

1
Comment actions Permalink

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

15
Comment actions Permalink

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: 

@Bean
public Generator generatorResolver() {
return new Generator();
}

In the Find in Path pop up, the regex I use is: 

@Bean\n.*generatorResolver.*

It works like a charm; thank you for the hints. 

 

0
Comment actions Permalink

Thanks Wendigoxs! [\s\S]* is the solution!

0
Comment actions Permalink

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:

(?:.|\n)*

Thought this was maybe worth recording here.

0

Please sign in to leave a comment.