473,394 Members | 1,951 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

match pattern of multiple lines


Hi,
I need to match the following pattern with a file,

begin <some string>
<some thing this line>
<some thing this line>
......
end

I check the web, it says I should use pattern modifier s & m, so I
write it this way,

while (<MyFile>){
if (/^(\s*)begin(.*)^(\s*)end/smi){
... call my function...
}
}

But it never matches the pattern, do you see the problem?

Thanks!

Jul 19 '05 #1
2 15842
John Black wrote:

while (<MyFile>){
if (/^(\s*)begin(.*)^(\s*)end/smi){
... call my function...
}
}

But it never matches the pattern, do you see the problem?


If you haven't set the $/ variable, you are only testing one line at a
time. One solution is to slurp the file instead of using a while loop:

local $_ = do { local $/; <MyFile> };

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Jul 19 '05 #2
On Thu, 15 Jul 2004 16:26:28 -0600, John Black wrote:

Hi,
I need to match the following pattern with a file,

begin <some string>
<some thing this line>
<some thing this line>
......
end

I check the web, it says I should use pattern modifier s & m, so I
write it this way,

while (<MyFile>){
if (/^(\s*)begin(.*)^(\s*)end/smi){
... call my function...
}
}

But it never matches the pattern, do you see the problem?

Thanks!


Hi,

This won't work because you are reading the file one line at a time. You
may want to read the whole file at one time by using this:

local $/; # slurp mode ( to read the file into a string )
my $content = <MyFile>;

if ( $content =~ /^\s*begin\s+(.+?)\s+end$/smi ) { CallFunc(); }

Also, check the regular expression you are using as it doesn't seem
to be right. I've modified it in the example above but I'm not sure if it
is what you want.

Note that reading the file at one time can be memory-intensive if the file
is too big.

good luck,
Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: BalyanM | last post by:
Hi, I am new to python.I am using it on redhat linux 9. I am interested to run python on a sun machine(SunE420R,os=solaris) with 4 cpu's for a pattern discovery/search program on biological...
9
by: Ron Adam | last post by:
Is it possible to match a string to regular expression pattern instead of the other way around? For example, instead of finding a match within a string, I want to find out, (pass or fail), if...
6
by: Matt Wette | last post by:
Over the last few years I have converted from Perl and Scheme to Python. There one task that I do often that is really slick in Perl but escapes me in Python. I read in a text line from a file...
1
by: RV | last post by:
Hi: Am having a huge performance problem in one of my scripts. I have an array containing some reference keys. ( about 1000 entries or so ). I also have a list of files ( about 100 or so )...
19
by: Tom Deco | last post by:
Hi, I'm trying to use a regular expression to match a string containing a # (basically i'm looking for #include ...) I don't seem to manage to write a regular expression that matches this. ...
8
by: sherifffruitfly | last post by:
Hi, I've been searching as best I can for this - coming up with little. I have a file that is full of lines fitting this pattern: (?<year>\d{4}),(?<amount>\d{6,7}) I'm likely to get a...
8
by: Natti | last post by:
Hello, I have a perl program which parses an input file for specific error patterns and generates an output file with all these lines. One of the error patterns that I am looking at spans across...
19
by: konrad Krupa | last post by:
I'm not expert in Pattern Matching and it would take me a while to come up with the syntax for what I'm trying to do. I hope there are some experts that can help me. I'm trying to match...
5
by: mikko.n | last post by:
I have recently been experimenting with GNU C library regular expression functions and noticed a problem with pattern matching. It seems to recognize only the first match but ignoring the rest of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.