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

The general rule is that I want to say, T has a method with
I'm relatively new to .NET programming (and OOP in general) and I want to
I want to loop over the contents of a text file and do a
I'm using Python and I want to use regular expressions to check if something
In my global.asax.cs file. I add an entry routes.MapRoute( Static text, Static/General/{filePath}, new {
I've looked at MongoDB/norm (but want a general document-oriented store answer). It seems really
I want to understand, in general, what this means: <xsl:template match=foo:barLists[@mode = 'Dummy Filter']
I want to create a hierarchy in my wiki like so: General FooPages Foo1
If all tables I want to delete from have the column gamer_id can i
I'm sort of new to regexs with Ruby, (or I suppose regex in general),

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.