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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:27:17+00:00 2026-06-13T20:27:17+00:00

I have a file like HEADER foo bar garbage SUBHEADER foo foo bar other

  • 0

I have a file like

HEADER foo bar
garbage
SUBHEADER foo foo bar
other garbage
SUBHEADER foo foo bar bar
HEADER foo baz
SUBHEADER foo bar foo foo
SUBHEADER foo foo foo foo
SOMETHING bar bar bar
HEADER baz baz
SUBHEADER baz bar baz foo

where the capitalized words occur literally and want to find SOMETHING together with the corresponding HEADER and SUBHEADER, i.e.,

HEADER foo baz
SUBHEADER foo foo foo foo
SOMETHING bar bar bar

It’s rather trivial in a program, but can a regex do? I could imagine a solution using negative assertions, but that get very unreadable.

  • 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-06-13T20:27:19+00:00Added an answer on June 13, 2026 at 8:27 pm

    If you’re looking for the nearest prior HEADER and SUBHEADER before the SOMETHING, then I think you just want non-greedy matching in your regex–assuming you have a regex processor that will match multiple lines at once, which generally rules out grep, sed, and similar.

    For example, something like this:

    (^HEADER.*?$).*?(^SUBHEADER.*?$).*?(^SOMETHING.*?$)
    

    I’m also assuming that ‘.‘ does match newlines (as in PCRE_DOTALL mode), and that ‘^‘/’$‘ will match beginning/end-of-line in the middle of the string (as in PCRE_MULTILINE mode). These are configurable options in many regex implementations.


    edit: I’ve modified the command you laid out in your comment and gotten it to work.

    perl -0777 -ne '/.*(^HEADER.*?\n).*(^SUBHEADER.*?\n).*?(^SOMETHING.*?\n)/ms
      and print "$1$2$3*\n"'
    

    (I added the ‘m’ flag and re-added beginning-of-line anchors for paranoia’s sake; you can take them back out if you want.)

    The key idea turned out to be placing a greedy match-all pattern at the beginning, giving the regular expression matcher permission to match HEADER as late as possible. I’d have expected an un-anchored match like this to act as if it had an implicit greedy match at the beginning, but apparently in the presence of non-greedy operators it doesn’t work that way.

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

Sidebar

Related Questions

I have a file like this: foo, bar, foo1 bar, foo foo I want
Basically I have code which looks like this inside a header file: class Bar;
If I have something like the following in a header file, how do I
I have a header file like this: #ifndef __GEN_NOTE_MARKERS_TO_DEVELOPERS_HPP__ #define __GEN_NOTE_MARKERS_TO_DEVELOPERS_HPP__ #ifdef _DEBUG //
I have a macro definition in header file like this: // header.h ARRAY_SZ(a) =
I have a file like this. rm a.txt mkdir foo cp a.doc docs I
Lets say i have two inline functions in my header file: inline int foo()
I have a class set up like this: class Foo { Foo(); private: Bar
Suppose you have the following file: textfield,datetimefield,numfield foo,2008-07-01 17:50:55.004688,1 bar,2008-07-02 17:50:55.004688,2 The Ruby code
Suppose that we have file like this: sometext11 sometext12 sometext13 sometext21 sometext22 sometext23 Texts

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.