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

  • Home
  • SEARCH
  • 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 3693238
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T04:21:16+00:00 2026-05-19T04:21:16+00:00

I have a text and I write a parser for it using regular expressions

  • 0

I have a text and I write a parser for it using regular expressions and perl.

I can match what I need with two empty lines (I use regexp), because there is a pattern that allows recognize blocks of text after two empty lines.

But the problem is that the whole text has Introduction part and some text in the end I do not need.

Here is a code which matches text when it finds two empty lines

#!/usr/bin/perl

use strict;
use warnings;

my $file = 'first';                    
open(my $fh, '<', $file);   
my $empty = 0;    
my $block_num = 1;    
open(OUT, '>', $block_num . '.txt');    

while (my $line = <$fh>) {  

 chomp ($line);
 if ($line =~ /^\s*$/) {  
  $empty++;      
  } elsif ($empty == 2) {     
   close(OUT);    
   open(OUT, '>', ++$block_num . '.txt');
   $empty = 0;
  } 
  else {
   $empty = 0;}
 print OUT "$line\n";

}
close(OUT);

This is example of the text I need (it’s really small :))


this is file example


I think that I need to iterate over the text till the moment it will find the word LOREM IPSUM with regexps this kind “/^LOREM IPSUM/”, because it is the point from which needed text starts(and save the text in one file when i reach the word).
And I need to finish iterating over the text when INDEX word is fount or save the text in separate file.

How could I implement it. Should I use next function to proceed with lines or what?

BR,
Yuliya

  • 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-19T04:21:16+00:00Added an answer on May 19, 2026 at 4:21 am

    You’d change your while loop to something like

    my $in_lorem = 0;
    while (my $line = <$fh>) {
      if( $line =~ /^LOREM IPSUM/ ) {
        $in_lorem = 1;
        next;
      }
      next unless $in_lorem;
      # your processing goes here
    }
    

    This will skip header lines until you hit the line that starts with LOREM IPSUM, after which you will process lines.

    You’d use a similar pattern for ignoring all lines after a given line match, except you wouldn’t have to process any more lines, so instead of using next you’d use last. That pattern is left as an exercise to the reader. 🙂

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

Sidebar

Related Questions

I have one text input and one button (see below). How can I use
I am trying to write a regular expression that can parse the text between
I have a textbox and a link button. When I write some text, select
I have to write a C parser for online blogs and different word manipulation
I have to write some sort of parser that get a String and replace
I'm looking to have text display vertically, first letter at the bottom, last letter
In a project we have text files looking like this: mv A, R3 mv
I'm trying to have text spans pop up on a hover pseudo-class for different
I have a text file on my local machine that is generated by a
I have a text file of this format: L O A D C A

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.