Home: Perl Programming Help: Regular Expressions:
Problem matching to pattern with variable



strathglass
New User

Jul 15, 2009, 6:46 AM


Views: 5550
Problem matching to pattern with variable

Trying to find lines like this in an input file to be processed:

Code
#include <//Server//path//VYE//users.SYS//filename.inc>

(so I can create a new output file with modified path info).

Here is my test:

Code
if ($line =~ m/^(#include[ \t]+<)$dxlSourcePath(.*)/i)

where $dxlSourcePath is set to:
"//Server//path//VYE//users.SYS"

It is not working.

What is confusing is the output from the following debug printout:

Code
printf("NO MATCH to pattern /^(#include[ \\t]+<)$dxlSourcePath(.*)/i for line='$line'\n");

The output from above is:

Code
NO MATCH to pattern /^(#include[ \t]+<)//Server//path//VYE//users.SYS///(.*)/i for line='#include <//Server//path//VYE//users.SYS//utils.inc>^M'


Notice the three trailing slashes at the end of the printed $dxlSourcePath value, before the string '(.*)':
where the heck did they come from??

Any help appreciated.

Regards, Strathglass.


KevinR
Veteran


Jul 15, 2009, 10:30 AM


Views: 5548
Re: [strathglass] Problem matching to pattern with variable

Works for me as-is. I suspect your problem is elsewhere but where I have no idea.


Code
$line = '#include <//Server//path//VYE//users.SYS//filename.inc>'; 
$dxlSourcePath = '//Server//path//VYE//users.SYS';
if ($line =~ m/^(#include[ \t]+<)($dxlSourcePath)(.*)/i){
print "It matches: [$1] - [$2] - [$3]\n";
}
else {
print "It does not match\n";
}


the [] brackets are there in the printout so you can see what is matching
-------------------------------------------------


strathglass
New User

Jul 15, 2009, 12:23 PM


Views: 5545
Re: [KevinR] Problem matching to pattern with variable

Thanks. Maybe it is something wacky with my SFU package on Windows.
I will try it out on a different platform (my Mac or ActiveState perl on Windows).

-strathglass