• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 242
  • Last Modified:

How to fix the new line matching in Perl


This is the line I try ot capture:

                Submit fileCRLF
        ===========================CRLF

Open in new window


or

		Submit fileLF
	===========================LF

Open in new window



However these matches does not work due to the new line character. I think the match should be more smart at this point.

I would like to cover Windows, Unix and Mac systems.

if (/^\s*Submit\s+file\s*$/)

Open in new window



if (/^\s*\=+\s*$/)

Open in new window


How can I fix this problem in my code?

Thanks,
0
Tolgar
Asked:
Tolgar
2 Solutions
 
wilcoxonCommented:
This will do what you want.  I tested it for all of dos/unix/mac.  All three are one or more of \n and \r.
#!/usr/local/bin/perl

# always use strict and warnings
use strict;
use warnings;

my $fil = shift or die "Usage: $0 file\n";
open IN, $fil or die "could not open $fil: $!";
while (<IN>) {
    if (m{^\s*Submit\s+file\s*[\r\n]+}) {
        print "got line $_\n";
    }
}

Open in new window

0
 
wilcoxonCommented:
I meant to say that the relevant line for what you want is line 10.
0
 
Carl BohmanCommented:
Those regexes should work.  What exactly are you seeing or not seeing?  Have you checked the value of $_ prior to calling the regex to ensure that the value you expect is in that variable?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
TolgarAuthor Commented:
I assume this will take care of the equal line

if (m{^\s*=+\s*[\r\n]+}) {
print "got line $_\n";
}

Open in new window


Am I right?

Thanks,

0
 
wilcoxonCommented:
Yes, that will take care of the equal line.

Although, I just did some quick checks based on bounsy's comment and it looks like the regexes as you had them originally should work (they work correctly under Cygwin perl 5.10.1 with dos/unix/mac at least).  Specifically, I used m{^\s*Submit\s+file\s*$} and it matched.

Also, have you tried using chomp on $_ prior to doing the match (shouldn't be necessary but should also fix any end-of-line problems (I think)).

Hmm.  I suppose the problem could come in if the file is in one format but perl (or the OS) think it's in a different format.  I just checked and "unix" format with dos eol seems to work but mac eol does not (I didn't test other combinations).
0
 
parparovCommented:
You can always strip the \r character before doing any matching by
s/\r//g;

Open in new window

at the beginning of the loop for the lines.
This would unify the code.
However \s* should match \r, \r\n,\n and their absence.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now