LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-13-2010, 07:25 AM   #1
smc2
LQ Newbie
 
Registered: Apr 2010
Posts: 29

Rep: Reputation: 0
Question 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";

}

Last edited by smc2; 10-13-2010 at 07:28 AM.
 
Old 10-13-2010, 08:19 AM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,395
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
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.

Last edited by theNbomr; 10-13-2010 at 08:21 AM.
 
  


Reply

Tags
perlscript, regular expressions


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with Regular Expression in VIM search and replace vijay_babu1981 Linux - Newbie 8 10-22-2009 07:49 AM
regular expression with perl MissEileen Programming 3 03-11-2008 01:26 PM
Perl Regular Expression abdul_zu Linux - Software 2 08-20-2007 03:57 AM
regular expression search/replace question (HELP!!! :) amytys Programming 5 09-06-2004 02:36 PM
Perl Regular Expression rch Programming 14 07-11-2003 11:00 PM


All times are GMT -5. The time now is 03:37 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration