PDA

View Full Version : [Perl] Difficulties with Regex



DishBreak
September 17th, 2009, 12:18 AM
Hi all,

I'm playing with a perl script that does a comparison of two files. Because the order can be jumbled between the files, a simple line-by-line diff won't work.

I've been using the grep() function in Perl as follows.


@matches = grep (/$searchTerm/, @fileLines);


This works well...except when the $searchTerm scalar contains special characters (i.e. $, +, etc.) Instead of taking these characters literally, they're taken to be significant to the the regex.

What can I do to make sure the string is taken literally? I'm almost at the end of my rope, so anything would help.

Mirge
September 17th, 2009, 12:44 AM
Hi all,

I'm playing with a perl script that does a comparison of two files. Because the order can be jumbled between the files, a simple line-by-line diff won't work.

I've been using the grep() function in Perl as follows.


@matches = grep (/$searchTerm/, @fileLines);
This works well...except when the $searchTerm scalar contains special characters (i.e. $, +, etc.) Instead of taking these characters literally, they're taken to be significant to the the regex.

What can I do to make sure the string is taken literally? I'm almost at the end of my rope, so anything would help.

http://www.anaesthetist.com/mnm/perl/Findex.htm#regex.htm



\Q quote (disable) pattern metacharacters till \E

ghostdog74
September 17th, 2009, 12:52 AM
if you want to use Perl to do comparison of files, you can use modules such as File::Compare. If Perl is not a must, you can sort your files using sort first, then use diff

DishBreak
September 17th, 2009, 06:06 AM
@Mirge: Thanks! That seemed to have solved my problem.

@ghostdog74: The files I'm comparing are configs for Cisco routers. the config is split into blocks of code. For example, each interface has its own code block, and the blocks contain similar commands. If the blocks are out of order in the file I'm comparing with, a line by line diff (which is what the File::Compare module looks like it's doing) will not work on its own. Neither will sorting the files.

It does seem to be a useful module, though. I'll likely use it in the future, I'm sure. Thanks! =)