CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
whats wrong in the regular expression

 



ianilkumar
Novice

Nov 16, 2012, 4:23 AM

Post #1 of 7 (2093 views)
whats wrong in the regular expression Can't Post

use File::Find;
use File::Copy;
use IO::Handle;


my $file="./product_priority_test/create_cust_prio.039.opc";
open (FILE,"<$file");
while (<FILE>)
{
chomp;
if ($_ =~ m/(\d\s+PIN_FLD_PASSWD_CLEAR\s+\[0\]\s+)(.*)$/g)
{
print $_;
}

}
close FILE;
}

If the contents of the file are :

# description: Create a invoice account, with the passed in name
1 PIN_FLD_PASSWD_CLEAR STR [0] "$(last_name)"

the regular expression in if is not working.

When i run it in the debug mode i get something like below:

Why these hexadecimal values are coming:

DB<2> n
Watchpoint 0: $_ changed:
old value: '# description: Create a invoice account, with the passed in name
'
new value: '# description: Create a invoice account, with the passed in name'
main::(1.pl:22): if ($_ =~ m/(\d\s+PIN_FLD_PASSWD_CLEAR\s+\[0\]\s+)(.*)$/g)
main::(1.pl:23): {
DB<2> n
Watchpoint 0: $_ changed:
old value: '# description: Create a invoice account, with the passed in name'
new value: '0030 0039
'
utf8::SWASHNEW(/pinhome/pin3056/opt/portal/7.5/lib/5.8.0/utf8_heavy.pl:153):
153: sort { hex $a <=> hex $b }
DB<2> n
Watchpoint 0: $_ changed:
old value: '0030 0039
'
new value: '0660 0669
'
utf8::SWASHNEW(/pinhome/pin3056/opt/portal/7.5/lib/5.8.0/utf8_heavy.pl:153):
153: sort { hex $a <=> hex $b }
DB<2>
Watchpoint 0: $_ changed:
old value: '0660 0669
'
new value: '06F0 06F9
'
utf8::SWASHNEW(/pinhome/pin3056/opt/portal/7.5/lib/5.8.0/utf8_heavy.pl:153):
153: sort { hex $a <=> hex $b }
DB<2>
Watchpoint 0: $_ changed:
old value: '06F0 06F9
'
new value: '0966 096F
'


rovf
Veteran

Nov 16, 2012, 7:13 AM

Post #2 of 7 (2090 views)
Re: [ianilkumar] whats wrong in the regular expression [In reply to] Can't Post

My guess: The content of the file is not what you think.

Do a print before the chomp.


ianilkumar
Novice

Nov 16, 2012, 9:16 AM

Post #3 of 7 (2081 views)
Re: [rovf] whats wrong in the regular expression [In reply to] Can't Post

It is the same. Not sure what is the issue.


Laurent_R
Veteran / Moderator

Nov 16, 2012, 9:59 AM

Post #4 of 7 (2079 views)
Re: [ianilkumar] whats wrong in the regular expression [In reply to] Can't Post

Is this Oracle BRM? It definitely looks like things I am working on.

Your regex should match the input you are providing, if I did not miss something.

Your regex could be made simpler:
- the capturing parens are useless since you are not caopturing anything, actually, the parens whether capturing or not are useless
- the final part ((.*)$) is also useless.
- the /g option is useless


Code
if ($_ =~ m/(\d\s+PIN_FLD_PASSWD_CLEAR\s+\[0\]\s+)(.*)$/g

could be rewritten as:


Code
if ( /\d\s+PIN_FLD_PASSWD_CLEAR\s+\[0\]\s+/) { #...



Laurent_R
Veteran / Moderator

Nov 16, 2012, 10:29 AM

Post #5 of 7 (2076 views)
Re: [ianilkumar] whats wrong in the regular expression [In reply to] Can't Post

Hi,

Actually, you are missing a part of your string in your regex, namely the STR datatype.

The regex should be:


Code
m/\d\s+PIN_FLD_PASSWD_CLEAR\s+STR\s+\[0\]\s+/;


And it works, as the following session under the Perl debugger shows:

Code
$ perl -de 42 

Loading DB routines from perl5db.pl version 1.32
Editor support available.

Enter h or `h h' for help, or `man perldebug' for more help.

main::(-e:1): 42
DB<1> $_ = '1 PIN_FLD_PASSWD_CLEAR STR [0] "$(last_name)" '

DB<2> print $_ if m/\d\s+PIN_FLD_PASSWD_CLEAR\s+STR\s+\[0\]\s+/;
1 PIN_FLD_PASSWD_CLEAR STR [0] "$(last_name)"
DB<3> q



ianilkumar
Novice

Nov 16, 2012, 4:06 PM

Post #6 of 7 (2063 views)
Re: [Laurent_R] whats wrong in the regular expression [In reply to] Can't Post

It worked after changing it to

$_ =~ s/(\d\s+PIN_FLD_PASSWD_CLEAR\s+STR\s+\[0\]\s+)(.*)/$1"$passwd"/g;
I wanted to use braces so that i can replace it.


Laurent_R
Veteran / Moderator

Nov 17, 2012, 12:35 AM

Post #7 of 7 (2054 views)
Re: [ianilkumar] whats wrong in the regular expression [In reply to] Can't Post

OK, in your originally posted code, the parens were useless, but if you want to perform a substitution, then, of course you need parens to capture the beginning of your line. Still, the "(.*)" part is useless.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives