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 3429144
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T07:02:46+00:00 2026-05-18T07:02:46+00:00

quite new to regexes i’m trying to optimize one, or at least know if

  • 0

quite new to regexes i’m trying to optimize one, or at least know if there are better ways to do it.

Here is my input string:

$str = 'Some text
spanned on
several lines
txt_to_grab1 fixed_text1 txt_to_grab2
Full line to grab
txt_to_grab3 fixed_text2 txt_to_grab4
Some text after';

I’m trying to grab the lines from “txt_to_grab1” to “txt_to_grab4”, but only the words “txt_to_grabX” and the line “Full line to grab”.
I want to preserve everything untouched before and after (ie line breaks), but remove line breaks inside the lines i grab (as each line will be a <tr> that’ll go into an html table).

Regex patterns/replace i found matching:

$find = "#(?<=\n)(.*?) fixed_text1 (.*?)(\n.*?\n)(.*?) fixed_text2 (.*?)(\n)#i";
$replace = '"$1" && "$2" grabbed.$3"$4" && "$5" grabbed.$6';   

$find = "#(.*)(?<=\n)(.*?) fixed_text1 (.*?)(\n)(.*)(?<=\n)(.*?) fixed_text2 (.*?)(\n.*)#is";
$replace = '$1"$2" && "$3" grabbed.$4$5"$6" && "$7" grabbed.$8';

Questions :

All questions can be sum up as : are there better/shorter/faster patterns ?

  • how to make the patterns work with either \r\n or \n ? I read somewhere on stack that (\r?\n) would be a solution, but i dunno how to use them in lookbehinds. For example the following patterns work, but i don’t like them (dirty as only \n are used in lookbehinds, may produce unexpected results):

    "#(?<=\n)(.*?) fixed_text1 (.*?)(\r?\n.*?\r?\n)(.*?) fixed_text2 (.*?)(\r?\n)#i"
    "#(.*)(?<=\n)(.*?) fixed_text1 (.*?)(\r?\n)(.*)(?<=\n)(.*?) fixed_text2 (.*?)(\r?\n.*)#is";
    
  • even better, how to use the “s” modifier to remove all line breaks from the pattern, so being able to use (.*?) but still grabbing what i want ? Word boundaries ?

  • is the multiline mode (m modifier) useful/helpful here ?

I’d really like the regexes to be explained, if you provide some 🙂

  • 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-18T07:02:46+00:00Added an answer on May 18, 2026 at 7:02 am

    You don’t need lookbehinds for this. Just use the start-of-line anchor at the beginning of your regex and the end-of-line anchor at the end (that’s ^ and $ in multiline mode). To match the line separators in the middle you can use (?:\r\n|[\r\n]), a common idiom for the three most common styles of line separator: \n, \r, or \r\n.

    As for the s modifier (a.k.a. “single-line” or “DOT_ALL”), you don’t need that either. All it does is allow the dot metacharacter to match line separators as well as all other characters, which doesn’t do you any good. You want it to stop matching when it reaches line breaks, so you can exclude them from your captures.

    Here’s a demo:

    $pattern='#^(.*?) fixed_text1 (.*)(?:\r\n|[\r\n])(.*)(?:\r\n|[\r\n])(.*?) fixed_text2 (.*)$#im';
    
    preg_match($pattern, $source, $m);
    
    echo "$m[1] && $m[2] grabbed.\n";
    echo "$m[3]\n";
    echo "$m[4] && $m[5] grabbed.\n"; 
    

    output:

    txt_to_grab1 && txt_to_grab2 grabbed.
    Full line to grab
    txt_to_grab3 && txt_to_grab4 grabbed.
    

    See it in action on ideone.com

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

Sidebar

Related Questions

Quite new to maven here so let me explain first what I am trying
Im quite new to spring mvc. What I'm trying to achive is separating static
I am quite new with jQuery and I don't know how to implement this
Hi I am quite new on MVC and I am trying to create a
I'm quite new to Ember and I'm quite lost to find the better approach
quite new to GWT i was wondering if there's any solution to automatically generate
I am quite new to windows forms. I would like to know if it
Im quite new to generics and as a learning exercise Im trying to create
I am quite new to Windows programming. I am trying to retrieve the name
Im quite new to HTML. What I am trying to do, is create a

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.