Imagine we have following PHP code:
if (true)
{
doSomething();
}
But we want to have:
if (true) {
doSomething();
}
I figure we can use the Perl in-place mass edit facility to make this sort of change across an entire codebase.
But I just cannot get it to match a newline followed by whitespace then a brace.
Here’s what I’m trying
perl -pi -w -e 's/if(.*)\n\s+\{/if$1 \{/g' testfile.php
I’m at a complete loss; it matches if I don’t include the whitespace and brace. But that’s not very helpful.
Using
-pmakes Perl loop over the lines of the file, one line at a time, thus preventing you to match content over several lines.You can use
-0to set the input record separator to null and make Perl read the whole file at once. (See perlrun.)You can try something like this:
Note that this isn’t a great solution for editing PHP. Strings, comments and the text content of the file are just a few things that would easily break such a simplistic solution. I suggest finding a right tool for the job. Some editors/IDEs have support for (re)formatting your code, maybe you can try one of those.