Tolgar
asked on
How to fix the new line matching in Perl
This is the line I try ot capture:
Submit fileCRLF
===========================CRLF
or
Submit fileLF
===========================LF
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*$/)
if (/^\s*\=+\s*$/)
How can I fix this problem in my code?
Thanks,
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I meant to say that the relevant line for what you want is line 10.
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?
ASKER
I assume this will take care of the equal line
Am I right?
Thanks,
if (m{^\s*=+\s*[\r\n]+}) {
print "got line $_\n";
}
Am I right?
Thanks,
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).
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).
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.