CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
Help on regular expression

 



navsaug
New User

Sep 10, 2008, 10:37 AM

Post #1 of 6 (3871 views)
Help on regular expression Can't Post

Hi -

Please help me with the following. I have to a parse a log file and here is the part of the log file which is occurs repetitively.

[Text1] [Text2] [Text3] [Text4] [Text5] [Text6] [Text7] [

===== PATTERN1 =======

Text8_line1: value1

Text8_line2: value2

Text8_line3: value3

Text8_line4: value4

.

.

.

Text8_linen: valuen

===== PATTERN1 =======

]

So basically I want to copy the text (i.e. Text1,Text2, ...... Text8) into different array or scalar variable for further processing. The brackets ([ and ]) for Text 8 are in different lines.

Please help me with this.

Thanks.

-Navsaug


sycoogtit
User

Sep 10, 2008, 1:41 PM

Post #2 of 6 (3866 views)
Re: [navsaug] Help on regular expression [In reply to] Can't Post

What have you tried so far?

--
http://bunsooter.com


navsaug
New User

Sep 10, 2008, 2:11 PM

Post #3 of 6 (3863 views)
Re: [sycoogtit] Help on regular expression [In reply to] Can't Post

Hi -

This is what I have tried and I am just getting the text between first square brackets:

#!/usr/bin/perl

$file = 'sample.data';

open(FILE, $file);
while(<FILE>)
{
chomp;
$text .= $_;
}
close(FILE);
$text=~m/\[(.*?)\]/s;
print $1;

Basically there 8 data items between corresponding square brackets (i.e. between [ & ]) which I need but the 8th one is on multiple lines ( which makes the square brackets for that item to be on different lines).

basically bottom line is I want the text between "[" (when its a first character on the line) and "]" (when this also is a first character on the line).

Thanks.

-Navsaug


sycoogtit
User

Sep 10, 2008, 2:20 PM

Post #4 of 6 (3860 views)
Re: [navsaug] Help on regular expression [In reply to] Can't Post

It looks like you've pretty much got it. You just need to loop through all the occurrences now, right? Try this:


Code
while ($text =~ s/\[(.*?)\]//s) { 
print "$1\n";
}


I'm making it substitute with nothing so the loop isn't infinite.

--
http://bunsooter.com


navsaug
New User

Sep 11, 2008, 11:10 AM

Post #5 of 6 (3820 views)
Re: [sycoogtit] Help on regular expression [In reply to] Can't Post

Thanks .. that worked a bit not exactly the way I wanted it to be. basically what I need is to extract the string between "[" and "]" when they are the first character on the lines i.e.

[ ABC ] [BCVD]......[

;lskds;l

asdlkd

]

so basicallly I want the text between RED brackets above i.e. [ and ] when they are first characters on the line.

Thanks again.

-Naveen


(This post was edited by navsaug on Sep 11, 2008, 11:11 AM)


sycoogtit
User

Sep 11, 2008, 12:21 PM

Post #6 of 6 (3813 views)
Re: [navsaug] Help on regular expression [In reply to] Can't Post

Ah. Thanks for the color coding. Smile How about this?


Code
while ($txt =~ s/(^|\n)\[(.*?)\n\]//s) { 
print "$2\n";
}


--
http://bunsooter.com

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives