#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    4
    Rep Power
    0

    Unmatched ( in regex;


    Hi all,

    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
  2. #2
  3. kill 9, $$;
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2001
    Location
    Shanghai, An tSín
    Posts
    6,897
    Rep Power
    3886
    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

    • Laurent_R agrees : I agree with everything.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    4
    Rep Power
    0
    OK, I just tried with Index and it works perfect. Thanks a lot ishnid

IMN logo majestic logo threadwatch logo seochat tools logo