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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T23:23:27+00:00 2026-05-18T23:23:27+00:00

I want 4 general RegEx expressions for the following 4 basic cases: Up to

  • 0

I want 4 general RegEx expressions for the following 4 basic cases:

  1. Up to A chars starting after B chars from start of line on up to C lines starting after D lines from start of file
  2. Up to A chars starting after B chars from start of line on up to C lines occurring before D lines from end of file
  3. Up to A chars starting before B chars from end of line on up to C lines starting after D lines from start of file
  4. Up to A chars starting before B chars from end of line on up to C lines starting before D lines from end of file

These would allow to select arbitrary text blocks anywhere in the file.

So far I have managed to come up with cases that only work for lines and chars separately:

  • (?<=(?m:^[^\r]{N}))[^\r]{1,M} = UP
    TO M chars OF EVERY LINE, AFTER FIRST
    N chars
  • [^\r]{1,M}(?=(?m:.{N}\r$))
    = UP TO M chars OF EVERY LINE, BEFORE LAST N chars

The above 2 expressions are for chars, and they return MANY matches (one for each line).

  • (?<=(\A([^\r]*\r\n){N}))(?m:\n*[^\r]*\r$){1,M} = UP TO M lines AFTER FIRST N lines
  • (((?=\r?)\n[^\r]*\r)|((?=\r?)\n[^\r]+\r?)){1,M}(?=((\n[^\r]*\r)|(\n[^\r]+\r?)){N}\Z) = UP TO M lines BEFORE LAST N lines from end

These 2 expressions are equivalents for the lines, but they always return just ONE match.

The task is to combine these expressions to allow for scenarios 1-4. Anyone can help?

Note that the case in the title of the question, is just a subclass of scenario #1, where both B = 0 and D = 0.

EXAMPLE 1: Characters 3-6 of lines 3-5. A total of 3 matches.

SOURCE:

line1 blah 1
line2 blah 2
line3 blah 3
line4 blah 4
line5 blah 5
line6 blah 6

RESULT:

<match>ne3 </match>
<match>ne4 </match>
<match>ne5 </match>

EXAMPLE 2: Last 4 characters of 2 lines before 1 last line. A total of 2 matches.

SOURCE:

line1 blah 1
line2 blah 2
line3 blah 3
line4 blah 4
line5 blah 5
line6 blah 6

RESULT:

<match>ah 4</match>
<match>ah 5</match>
  • 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-18T23:23:27+00:00Added an answer on May 18, 2026 at 11:23 pm

    For starters, here’s an answer for “Basic Case 1”:

    Regex regexObj = new Regex(
        @"(?<=            # Assert that the following can be matched before the current position
         \A               # Start of string
         (?:.*\r\n){2,4}  # 2 to 4 entire lines (D = 2, C = 4+1-2)
         .{2}             # 2 characters (B = 2)
        )                 # End of lookbehind assertion
        .{1,3}            # Match 1-3 characters (A = 3)", 
        RegexOptions.IgnorePatternWhitespace);
    

    You can now iterate over the matches using

    Match matchResults = regexObj.Match(subjectString);
    while (matchResults.Success) {
        // matched text: matchResults.Value
        // match start: matchResults.Index
        // match length: matchResults.Length
        matchResults = matchResults.NextMatch();
    }
    

    So, in the text

    line1 blah 1
    line2 blah 2
    line3 blah 3
    line4 blah 4
    line5 blah 5
    line6 blah 6
    

    it will match

    ne3
    ne4
    ne5
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm after a general regex for sanitising form input, I want to use it
I want to make a general error handling package that should be called from
I want to translate an XML file with data like the following: <FlatData> <Details1_Collection>
It seems I still don't get generics ... I want a general function to
We want to compare general performance (CPU, I/O, network, ...) of different JVMs for
I want to know the General formula for Writing Regular Expression? Any article?
i just want to know a general information about this particular information. Any good
I have one very general object that I want to map to a destination
We have an application that we want to make available to the general internet-using
want to open pdf file when a user clicks on hyperlink shown in gridview

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.