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.

8
11 comments

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:

3

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

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

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

0

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

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

32

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

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

2

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.

3

If you are trying to make the . character match newlines (similar to DOTALL mode in Java) you may find this useful.

As the docs say:

IntelliJ IDEA supports all the standard regular expressions syntax

This means you can use modifiers such as (?s) a the start of the expression which:

makes the dot match all characters, including line breaks

9

Please sign in to leave a comment.