November 4th, 2013, 06:38 AM
Unmatched ( in regex;
I have a problem with the "(" i am trying to concatenate.
Here is the code :
my $pattern = "set_TRC".' ( '.$TRC3_ADDR;
print "$pattern \n";
and in a function I do a :
last if $line =~ m/$pattern/;
and the error message :
set_TRC ( 0x5a
Unmatched ( in regex; marked by <-- HERE in m/set_TRC ( <-- HERE 0x5a/ at ./mbist_pattern_check.pl line 68, <FILE> line 1.
Line 68 is the line where I am doing the "last if $line =~ m/$pattern/;"
TRC3_ADDR is 0x5a, so it seems to work because "set_TRC ( 0x5a" is exactly what I mean. But the problem is still there. I tried with :
my $pattern = "set_TRC \( ".$TRC3_ADDR;
But the error message is the same. Help please
November 4th, 2013, 06:58 AM
You're running into the problem of using double quotes to quote regular expressions.
The string you use in your pattern match needs to have a backslash before the opening parenthesis. However, within a double-quoted string, '\(' is turned into simply '('. Try printing your $pattern to see what I mean. Within a double-quoted string, you need to have two backslashes to result in a backslash character being included in the string. A single-quoted string would work for you, or even better use the qr// quote (see perlop for details), which is designed to quote regexps.
BUT, more importantly, the thing you're trying to match isn't actually a pattern at all: you're looking for a literal string within another. If that's the case then you don't need to bother with regexps: simply use the 'index' function to find whether one string contains another (it will return -1 if it's not found, and something else (the position of the substring within the string) if it is). This would be a better way to do it.
Comments on this post
November 4th, 2013, 07:39 AM
OK, I just tried with Index and it works perfect. Thanks a lot ishnid