Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6562709
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:44:28+00:00 2026-05-25T13:44:28+00:00

Imagine we have following PHP code: if (true) { doSomething(); } But we want

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T13:44:28+00:00Added an answer on May 25, 2026 at 1:44 pm

    Using -p makes 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 -0 to set the input record separator to null and make Perl read the whole file at once. (See perlrun.)

    You can try something like this:

    perl -p0iwe 's/\bif\b(.*)\n\s*\{/if$1 \{/g' test.php
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Imagine we have 2 files, one called 1.php with the following code: <?php $hello
Imagine I have the following code (simplified regarding my real context of course): <div
I came across the following weird chunk of code.Imagine you have the following typedef:
This is probably a silly question, but here it goes.Imagine you have the following
Imagine that i have the following code: <a:repeat value=#{bean.getList()} var=x > <li class=la> <span>
Imagine the following REBOL code: foo: context [bar: 3] I now have a context
Imagine I have the following code that runs as a background processor for an
Imagine I have the following string : set(SEXY_STRING I love CMake) then I want
Imagine I have the following situation: File1.php <?php include(Function.php); log(test); ?> Function.php <?php function
Imagine I have the following code: class A { [UsefulAttribute(foo)] B var1; [UsefulAttribute(bar)] B

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.