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



ianilkumar
Novice

Nov 16, 2012, 4:23 AM


Views: 4181
whats wrong in the regular expression

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


Views: 4178
Re: [ianilkumar] whats wrong in the regular expression

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


Views: 4169
Re: [rovf] whats wrong in the regular expression

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


Laurent_R
Veteran / Moderator

Nov 16, 2012, 9:59 AM


Views: 4167
Re: [ianilkumar] whats wrong in the regular expression

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


Views: 4164
Re: [ianilkumar] whats wrong in the regular expression

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


Views: 4151
Re: [Laurent_R] whats wrong in the regular expression

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


Views: 4142
Re: [ianilkumar] whats wrong in the regular expression

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.