One thing I would add is that the inner pair of parentheses is used to
isolate ^|\n\n from the rest of the pattern so it is clear what the | is
operating on. /cat|blue fish/ matches "cat" or "blue fish" but
/(cat|blue) fish/ matches "cat fish" or "blue fish". (The parentheses
also capture the thing that was matched in $2, but we don't need it.
(?:^|\n\n) would prevent it from capturing the match, but capturing
doesn't hurt anything in this case and the pattern is easier to read.)

Katy


Nigel Peck wrote:
> 
> s/((^|\n\n).+?)\n/$1 /g;
> 
> s/       substitute
> (         put this is $1
> (^      the start of the file
> |          or
> \n\n)  two newlines
> ..+?      followed by 1 or more characters up until a
> )         stop putting it in $1
> \n      newline
> /         with
> $1 /    the text captured above followed by a space
> g;        keep doing it until you can't match anymore
> 
> >>> Sunish Kapoor <[EMAIL PROTECTED]> 07/04/02 04:27pm >>>
> Dear Katy,
> 
> It works great after the changed the regular expression to as given by
> u
> below..!
> Thanks a ton for the help.....but may I request for a small explanation
> of the
> line
> $file =~ s/((^|\n\n).+?)\n/$1 /g;
> 
> Sunish
> 
> Katy Brownfield wrote:
> 
> > Or, instead of changing the input file, change the regular expression
> to
> > also detect the beginning of the string.
> >
> > $file =~ s/((^|\n\n).+?)\n/$1 /g;
> >
> > Katy
> >
> > Timothy Johnson wrote:
> > >
> > >
> > > I think you'll need 2 blank lines.
> > >
> > > -----Original Message-----
> > > From: Sunish Kapoor
> > > To: Nigel Peck
> > > Cc: [EMAIL PROTECTED]
> > > Sent: 7/3/02 6:51 PM
> > > Subject: Re: Regular Expression Help sought
> > >
> > > Dear Nigel,
> > >
> > > Thanks a ton for the script..It works fine though it skips the
> first
> > > record
> > > even though I make
> > > the file begin with a blank line by simply hitting enter !
> > >
> > > Regards
> > >
> > > Sunish
> > > Nigel Peck wrote:
> > >
> > > > My first attempt, which may be a bit simplified, would be to
> > > substitute
> > > > any newline, which occured at the end of a line which was
> preceeded by
> > > 2
> > > > consecutive newlines, with a space. This relies on the format of
> the
> > > > file being as shown for every company. It also may miss the
> first
> > > > company in the file if the file doesn't start with a blank line.
> > > >
> > > > So
> > > >
> > > > undef $/;
> > > > $file = <FILE>;
> > > > $file =~ s/(\n\n.+?)\n/$1 /g;
> > > > print $file;
> > > >
> > > > [untested]
> > > >
> > > > HTH
> > > > Nigel
> > > >
> > > > >>> [EMAIL PROTECTED] 07/03/02 10:52pm >>>
> > > > Hi  All,
> > > >
> > > > I am new to Perl and need help to solve this one !
> > > >
> > > > I have a txt file and the contents of the file are as below  :
> > > >
> > > > ---------------CONTENTS   OF   TEXT
> FILE--------------------------
> > > > Abdullah Ahmed Hassan
> > > > Trading
> > > > Location Ruwi Souk St, Ruwi
> > > > Bus Hrs 0930-1300:1630-2200
> > > > P.O. Box 197 Ruwi Code 112
> > > > Phone 708940
> > > > Fax 794156
> > > > Key Staff Abdullah Ahmed Hassan
> > > > Mohammad Hussain Ahmed Hassan
> > > > Irfan Ahmed
> > > > Mohammad Asim
> > > > Activities :3410
> > > > Email : [EMAIL PROTECTED]
> > > >
> > > > Abu Ali Trading & Cont
> > > > Est
> > > > Location Azaiba
> > > > B us H rs 0700-1200; 1500-1900
> > > > P.O. Box 1695 C. P. 0. Code 111
> > > > Phone 595324
> > > > Key Staff K. Sasi, Prop
> > > > Ali Suleiman Al Ghubshi, Sponsor
> > > > Activities 2700
> > > >
> > > > Abu Al Dahab Trading & Contg
> > > > Est
> > > > Location 23rd July St, Opp Al Ghobish
> > > > Furniture Showroom
> > > > Bus Hrs 0800-1300 (Sat-Thu); 1600-1900 (Sat-
> > > > Fri)
> > > > P.O. Box 1254 Salalah Code 211
> > > > Phone 297879
> > > > Fax 297879
> > > > Key Staff Syed Tajudeen Madani, Gen Mgr
> > > > Activities 2660
> > > > ---------------CONTENTS  OF TEXT FILE--------------------------
> > > >
> > > > The names of three companies as above are:
> > > >
> > > > Abdullah Ahmed Hassan
> > > > Trading
> > > >
> > > > Abu Ali Trading & Cont
> > > > Est
> > > >
> > > > Abu Al Dahab Trading & Contg
> > > > Est
> > > >
> > > > I want the data this way .
> > > >
> > > > -----------DATA WANTED THIS WAY----------------
> > > > Abdullah Ahmed Hassan Trading
> > > > Location Ruwi Souk St, Ruwi
> > > > Bus Hrs 0930-1300:1630-2200
> > > > P.O. Box 197 Ruwi Code 112
> > > > Phone 708940
> > > > Fax 794156
> > > > Key Staff Abdullah Ahmed Hassan
> > > > Mohammad Hussain Ahmed Hassan
> > > > Irfan Ahmed
> > > > Mohammad Asim
> > > > Activities :3410
> > > > Email : [EMAIL PROTECTED]
> > > >
> > > > Abu Ali Trading & Cont Est
> > > > Location Azaiba
> > > > B us H rs 0700-1200; 1500-1900
> > > > P.O. Box 1695 C. P. 0. Code 111
> > > > Phone 595324
> > > > Key Staff K. Sasi, Prop
> > > > Ali Suleiman Al Ghubshi, Sponsor
> > > > Activities 2700
> > > >
> > > > Abu Al Dahab Trading & Contg Est
> > > > Location 23rd July St, Opp Al Ghobish
> > > > Furniture Showroom
> > > > Bus Hrs 0800-1300 (Sat-Thu); 1600-1900 (Sat-
> > > > Fri)
> > > > P.O. Box 1254 Salalah Code 211
> > > > Phone 297879
> > > > Fax 297879
> > > > Key Staff Syed Tajudeen Madani, Gen Mgr
> > > > Activities 2660
> > > > -----------DATA WANTED THIS WAY----------------
> > > >
> > > > I want PERL to make the name of the company to come in
> > > > one line as it has done for all three records.
> > > >
> > > > I request your help solving this.
> > > >
> > > > Regards
> > > >
> > > > Sunish Kapoor
> > > >
> > > > ITM Business Solutions
> > > > Unit 4
> > > > Nine Trees Trading Estate
> > > > Morthen Road
> > > > Rotherham
> > > > S66 9JG
> > > >
> > > > Reception
> > > > Tel: 01709 703288
> > > > Fax: 01709 701549
> > > >
> > > > Help Desk
> > > > Tel:01709 530424
> > > > Fax: 01709 702159
> > > >
> > > > CONFIDENTIALITY NOTICE: This message is intended only for the use
> of
> > > > the individual or entity to which it is addressed, and may
> contain
> > > > information that is privileged, confidential and exempt from
> > > disclosure
> > > > under applicable law.
> > > >
> > > > --
> > > > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > > > For additional commands, e-mail: [EMAIL PROTECTED]
> > >
> > > --
> > > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > > For additional commands, e-mail: [EMAIL PROTECTED]
> > >
> > > --
> > > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > > For additional commands, e-mail: [EMAIL PROTECTED]
> >
> > --
> > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > For additional commands, e-mail: [EMAIL PROTECTED]
> 
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 
> ITM Business Solutions
> Unit 4
> Nine Trees Trading Estate
> Morthen Road
> Rotherham
> S66 9JG
> 
> Reception
> Tel: 01709 703288
> Fax: 01709 701549
> 
> Help Desk
> Tel:01709 530424
> Fax: 01709 702159
> 
> CONFIDENTIALITY NOTICE: This message is intended only for the use of
> the individual or entity to which it is addressed, and may contain
> information that is privileged, confidential and exempt from disclosure
> under applicable law.
> 
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to