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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T03:14:56+00:00 2026-05-25T03:14:56+00:00

I have the following text layout: Heading Chapter 1:1 This is text 2 This

  • 0

I have the following text layout:

Heading
Chapter 1:1 This is text
2 This is more text
3 This is more text
4 This is more text
5 This is more text
6 This is more text
7 This is more text
8 This is more text
9 This is more text
10 This is more text
11 This is more text
12 This is more text
13 This is more text
14 This is moret text 
15 This is more text
Heading    
Chapter 2:1 This is text
2 This is more text...

and I am trying to add the first Chapter reference and the last one in that Chapter right after the Heading, written in parentheses. Like so:

Heading (Chapter 1:1-15)
Chapter 1:1 This is text
2 This is more text
3 This is more text
4 This is more text
5 This is more text
6 This is more text
7 This is more text
8 This is more text
9 This is more text
10 This is more text
11 This is more text
12 This is more text
13 This is more text
14 This is moret text 
15 This is more text

I’ve come up with this regular expression so far:

~s/(?s)(Heading)\r(^\d*\w+\s*\d+:\d+|\d+:\d+)(.*?)(\d+)(.*?\r)(?=Heading)/\1 (\2-\4)\r\2\3\4\5/g;

but this is grabbing the first number right after Chapter 1:1 (i.e. “2”, “Heading (Chapter 1:1-2)”), instead of the last one (“15” as in “Heading (Chapter 1:1-15)”). Could someone please tell me what’s wrong with the regex? Thank you!

  • 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-25T03:14:56+00:00Added an answer on May 25, 2026 at 3:14 am

    Edit for updated question

    Here’s a regex with explanation that will solve your problem. http://codepad.org/mSIYCw4R

    ~s/
    ((?:^|\n)Heading)   #Capture Heading into group 1.
                        #We can't use lookbehind because of (?:^|\n)
    (?=                 #A lookahead, but don't capture.
      \nChapter\s       #Find the Chapter text.
      (\d+:\d+)         #Get the first chapter text. and store in group 2
      .*                #Capture the rest of the Chapter line.
      (?:\n(\d+).+)+    #Capture every chapter line.
                        #The last captured chapter number gets stored into group 3.
    )
    /$1 (Chapter $2-$3)/gx;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have the following text file: This is the first line of
I have the following text tooooooooooooon According to this book I'm reading, when the
I have a text file that has the following layout: text text .. CUT
I have the following layout. This defines a row in my ListView. I noticed
I have following HTML layout <html> <head> <title></title> <meta http-equiv=Content-Type content=text/html; charset=UTF-8> <style type=text/css>
I have the following (simplified) layout: <header> ... more stuff here <nav id=nav> <div
I have an application using the following layout : alt text http://img15.imageshack.us/img15/238/screenshot003xbo.png When the
I have following text in a file 23456789 When I tried to replace the
I have the following text: <i><b>It is noticeably faster.</b></i> <i><b>They take less disk space.</i>
I have the following text from an academic course I took a while ago

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.