LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Perl Scripting: Search for Regular Expression not work :( (https://www.linuxquestions.org/questions/programming-9/perl-scripting-search-for-regular-expression-not-work-837780/)

smc2 10-13-2010 07:25 AM

Perl Scripting: Search for Regular Expression not work :(
 
I'm writing a Perl script to find an old key in a file and replace it with a new code
first the program should find the old key in the input file. here is the way I used in my script. but it doesn't work.
May you please let me know what is wrong and how I can correct it?
the key is stored in the file in the following format:
PHP Code:

Key=("1234567" someOtherVrable

I want 1234567
here is my script to find this key
Code:

open(INPUT,"inputfile.txt") or die "cant open file";
$oldKey=NULL;
while (<INPUT> == NULL && $oldKey == NULL) {
$_=~ m/key\(\"(\S*)/ig;
if ($1 != NULL ){
        $oldKey=$1;
print "The Old Key is  is $1";

}


theNbomr 10-13-2010 08:19 AM

Looks like your regex should be more like
Code:

/Key=\(\"([^"]+)\"/
This should match any characters enclosed in quotes, and qualified by the leading 'Key=(' string. You could further qualify the match with trailing context, possibly including beginning-of-line and/or end-of-line anchors as well. If the key is always alphanumeric, you could replace the 'anything-not-a-quote' regex with '\d'.

--- rod.


All times are GMT -5. The time now is 02:50 PM.