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

  • SEARCH
  • Home
  • 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 4093288
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T19:35:58+00:00 2026-05-20T19:35:58+00:00

I just answered a question here , which has left me asking my own

  • 0

I just answered a question here, which has left me asking my own question.

Basically, the OP wanted any line to be removed if it contained a string. Here is a regex I came up with…

$str = preg_replace('/^.*\b["(]?hello["(]?\b.*$/m', '', $str);

It works great, but because $ matches before the trailing \n, they remain when replaced and there are blank lines, e.g.

string(90) "
What it shouldhello do is:


could be words in between brackets and inverted commas also."

I can’t use \z because I’m in multiline mode (at least that is what I think).

If I use s modifier, the . become too greedy and don’t work across the newlines.

I have tried a few things (such as [^\n] and [\s\S]), and now I am stumped.

How can I match that trailing \n here so it is removed with the replace?

  • 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-20T19:35:58+00:00Added an answer on May 20, 2026 at 7:35 pm

    Use \n instead of $.

    $str = preg_replace('/^.*\b["(]?hello["(]?\b.*\n/m', '', $str);
    

    This misses the last line if it had hello in it, so here’s a step further.

    $str = preg_replace('/^.*\b["(]?hello["(]?\b.*(\n|$)/m', '', $str);
    

    The issue now is that the last line is removed, but there is still a \n character (similar issue you’re already having).

    Note: I’m not a regular expression expert at all, just can usually do enough for my needs.

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

Sidebar

Related Questions

Earlier, I answered this question , which was basically about removing a table row.
I just answered a question where I said that while string interning is good
A Java version of this question was just answered, and, well, I don't know
This is a follow up from a question of mine that was just answered
I just had a ridonkulous typo in my iPhone app, answered here . Now
This question has been asked on here a few times, but none of the
Update: Ehh -- Even though this question isn't answered, I've just emptied my pockets
This follows this prior question, which was answered. I actually discovered I could remove
I started out with this question , which I sort of answered there ,
This question was previously asked Here , but not answered, And failed to find

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.