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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T10:07:18+00:00 2026-05-30T10:07:18+00:00

Sometimes the good old tools still work best. In sed, I could write things

  • 0

Sometimes the good old tools still work best. In sed, I could write things like this:

sed '/^Page 5:/,/^Page 6:/p' 
sed '110,/^Page 10:/+3p'
sed '/^Page 5:/,/^Page 6:/s/this/that/g' 

The first applies a substitution to all lines between the ones matching /^Page 5:/ and /^Page 6:/. The second starts printing at line 110 and stops 3 lines after the one matching /^Page 10:/. The third example applies a substitution to each line in the specified range.

I don’t mind using re.search to search line by line, but for line ranges, line numbers or relative offsets, I end up having to write a whole parser. Is there a python idiom or module that can simplify this kind of operations?

I don’t want to call sed from python: I’m doing python-type things with text, and just want to be able to operate on line ranges in a straightforward way.

Edit: It’s fine if the solution works on a python list of strings. I’m not looking to process gigabytes of text. But I do need to specify several operations, not just one, and interleave them with single-line regexp substitutions. I’ve looked at iterators (in fact I would welcome a solution using iterators), but the results always got out of hand for anything more than single operation.

Here’s a simple example: A snippet of code with java-style comments, to be changed to python comments. (Don’t worry I am NOT trying to write a cross-compiler using regexps 🙂

/* 
 This is a multi-line comment.
 It does not obligingly start lines with " * "
 */

x++;  // a single-line comment

It’s trivial to write regexps that change “//” comments to “#” (and also to drop semicolons, change “++” to “+= 1”, etc.) But how do we insert “#” at the start of each line of a multi-line java comment? I can do it with a regexp on the entire file as a single string, which is a pain because the rest of the transformations are line-oriented. I’ve also been unable to (usefully) integrate iterators with line-oriented regexps. I’d appreciate suggestions.

  • 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-30T10:07:19+00:00Added an answer on May 30, 2026 at 10:07 am

    I would try to use the regex flags re.DOTALL or re.MULTILINE.

    The first treats newlines as regular characters, so if you use .* it might count newlines inside the pattern.

    The second is almost the same, but you can still use linestarts (^) and endlines ($) to match these. This can be useful to count lines.

    I could, for now, come up with this, which prints ONE MORE LINE after the ocurrence of “six” (a whole line is captured by the final ^.*?$, but I’m pretty sure there should be a much better way):

    import re
    
    source = """one
    two
    three
    four
    five
    six
    seven
    eight
    nine
    ten"""
    
    print re.search('^three.*six.*?^.*?$', source, re.DOTALL|re.MULTILINE).group(0)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When is it a good idea to use PHP_EOL ? I sometimes see this
Good Day Guys, I know this may sound like a stupid question. However, I
Sometimes coloring a logfile or other gives a good overview when looking for stuff
Having friendly URLs is generally a good thing. However, there are sometimes when it
Sometimes when I'm editing page or control the .designer files stop being updated with
Sometimes I have to work on code that moves the computer clock forward. In
There is a subtle reason why it might not be good: Sometimes, the blame
I generally use cout and cerr to write text to the console. However sometimes
I am good with ideas, but due to my limited programming experience I sometimes
I'm converting double to float using ye old float myFloat = (float)myDouble . This

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.