Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Regex Issue

by bluesplay106 (Novice)
on Aug 01, 2013 at 15:45 UTC ( #1047477=perlquestion: print w/replies, xml ) Need Help??
bluesplay106 has asked for the wisdom of the Perl Monks concerning the following question:

So I have a big text file that follows this pattern :
========== Name: blah ID: 2 ==========
and this repeats multiple times in the text file. I want to regex anything between the "=======" but only such that ID is say 3. I tried the following :
while($regex =~ /=====.+?ID: 3.+?=====/igs) { print "$&\n"; }
I want the output to look like :
========= Name: Hi ID: 3 =========
But I end up with the whole text file or at least it might be grabbing anything before ID: 3 and after the first "======", but I can't tell since the output is so large. I think that's what it might be doing. Also, what if there are more than one instance of ID: 3? Is there anyways I can print all those instances without printing the whole file? Can anyone help me?

Replies are listed 'Best First'.
Re: Regex Issue
by choroba (Bishop) on Aug 01, 2013 at 15:53 UTC
    If there is a record delimiter, use it! Read the file record by record, and output only those that contain the wanted ID:
    #!/usr/bin/perl use warnings; use strict; { local $/ = "==========\n"; while (<DATA>) { print "$/$_" if /ID: 3$/m; } } __DATA__ ========== Name: blah ID: 2 ========== Name: blah ID: 2 ========== Name: here ID: 3 ========== Name: blah ID: 2 ==========
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      Would that work if there were multiple instances of ID: 3?
        Why don't you try it?


        A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

        My blog: Imperial Deltronics

        Hi bluesplay106,
        You wouldn't break a thing by trying out the solution you have been given by choroba. Change some of the ID given in that solution to 3 and try it out.

        If you tell me, I'll forget.
        If you show me, I'll remember.
        if you involve me, I'll understand.
        --- Author unknown to me

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1047477]
Front-paged by Arunbear
[Corion]: marto: I used Padre for some time but then switched to Notepad++, since all the funky features I wanted to put into Padre couldn't be made to work, and/or I lost interest :)
[Discipulus]: indeed marto I dunno. I tested once and many years ago. But I'm not the programmer yardstick. I try to install Padre via cpan now just to add a line about it in the issue. Strawberryperl all life long!
[marto]: yeah, when I worked on Windows I did use Np++ all the time

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (11)
As of 2018-06-25 08:56 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.