Greenguy's Board

Greenguy's Board (http://www.greenguysboard.com/board/index.php)
-   Programming & Scripting (http://www.greenguysboard.com/board/forumdisplay.php?f=15)
-   -   Controlling REGEX in Perl? (http://www.greenguysboard.com/board/showthread.php?t=35209)

Jeremy 2006-10-11 10:21 PM

Controlling REGEX in Perl?
 
Gotta q for the perl gurus who are much much wiser than me.

When you read data into an array, if that data has a REGEX character in it, PERL handily recognises it and acts on it if you want to compare that array against another array of data.

EG if the first array has "foo.*bar" then if the second array has "foofumbar" and "foo.*bar" in it PERL will find them.....

BUT what if you actually wanted to search that second list for "foo.*bar" literally and ignore "foofumbar" completely, how do you do that?

JK 2006-10-11 11:24 PM

I'm not a guru mate, but I think all you'd need to do is escape the 'special' characters. foo.*bar literally should just be foo\.\*bar

Jeremy 2006-10-11 11:40 PM

That'd work mate... but it's a bit of a long way round - especially if you don't quite know what you're going to find in the data you're analysing.

EG, when I've analysed link descriptions, I've run into problems where descriptions have things like: "?" or "+" or even ":-)" smilies have caused the script to grind to a halt..... so it needs to be flexible enough to deal with whatever gets thrown it's way.

I'm sure that the answer's staring me in the face in PERLOP, but I'll be buggered if I can spot it!

JK 2006-10-12 12:04 AM

Aaah ok, I see where you're coming from now mate. Sorry I can't be of help.

cd34 2006-10-12 08:59 AM

how about the index function?

Code:

#!/usr/bin/perl

$string1 = "asdf asdf foofumbar asdf asdf";
$string2 = "asdf asdf foo.*bar asdf asdf";
$find = "foo.*bar";

print "string1: " . index($string1,$find) . "\n";
print "string2: " . index($string2,$find) . "\n";


Jeremy 2006-10-12 09:10 PM

Thanks for that Chris - will look into it.

This is actually the way that I'm using it at the moment:

Obviously it's just an example, but I've seen odd behaviour when data from either array has regex or pattern type characters in them.

Code:

#!/usr/bin/perl -w


## This Array is the orginal data to be searched:
## Data in here may have regex / pattern characters.
open (FH, "$WorkingDir/FILENAME");
while ()
        {
                chomp $_;
                push(@Data_Array, "$_");
        }
close (FH);


## This Array contains the things we want to look for
## in the Data_Array:
## Data in here may have regex / pattern characters.

open (FH2, "$WorkingDir/FILENAME");
while ()
        {
                chomp $_;
                push(@Look_For_Array, "$_");
        }
close (FH2);

foreach $Data_Array(@Data_Array)
{
        foreach $Look_For_Array(@Look_For_Array)
        {
                if ($Data_Array =~ /$Look_For_Array/i)
                {
                        $Data_Array =~ s/$Look_For_Array/$Something_Else/gmi;
                }
        }
}

Maybe I should be looking in Perl OP at:
# Quote and Quote-like Operators
# Regexp Quote-Like Operators

& using q somewhere may be the answer?


All times are GMT -4. The time now is 08:08 AM.

Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
© Greenguy Marketing Inc