Help on Regex

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rajiv07
    New Member
    • Jun 2007
    • 141

    #1

    Help on Regex

    Hi to all,

    I have regex related pl fie below.when i execute the file it gives some error.

    [CODE=perl]#!/usr/bin/perl
    my @array=('324234 2','fdsfsd4432' );

    my $file='D:/HTTP/sample/SAREGAMA/HINDI/01/INH100155170 O HANSINI(ZEHREEL A INSAAN.wma';

    my @ee=grep(/^$file$/,@array);

    print @ee;[/CODE]


    My error

    Unmatched ( in regex; marked by <-- HERE in m/^D:/HTTP/sample/SAREGAMA/HINDI/01/INH100155170 O HANSINI( <-- HERE ZEHREELA INSAAN.wma$/ at C:\DOCUME~1\IND IAM~1.COM\LOCAL S~1\Temp\locB8. tmp line 9.

    Please Help on this.

    regards Rajiv.
  • rajiv07
    New Member
    • Jun 2007
    • 141

    #2
    I got the point now if the string contain regex special character like $,^ ) we have to escape the character.

    Regards
    Rajiv

    Comment

    • nithinpes
      Recognized Expert Contributor
      • Dec 2007
      • 410

      #3
      Originally posted by rajiv07
      Hi to all,

      I have regex related pl fie below.when i execute the file it gives some error.

      [CODE=perl]#!/usr/bin/perl
      my @array=('324234 2','fdsfsd4432' );

      my $file='D:/HTTP/sample/SAREGAMA/HINDI/01/INH100155170 O HANSINI(ZEHREEL A INSAAN.wma';

      my @ee=grep(/^$file$/,@array);

      print @ee;[/CODE]


      My error

      Unmatched ( in regex; marked by <-- HERE in m/^D:/HTTP/sample/SAREGAMA/HINDI/01/INH100155170 O HANSINI( <-- HERE ZEHREELA INSAAN.wma$/ at C:\DOCUME~1\IND IAM~1.COM\LOCAL S~1\Temp\locB8. tmp line 9.

      Please Help on this.

      regards Rajiv.
      Change this line:
      Code:
      my @ee=grep(/^$file$/,@array);
      to :

      Code:
      my @ee=grep(/^\Q$file\E$/,@array);
      In your case, $file contains forward slashes which will mess up the regex(as they stand for pattern delimiters). Even using double quotes and escaping the slashes will not work.
      If you put a pattern between \Q and \E, any special meaning of the characters in pattern match will be ignored (variable substitutions will be allowed). For ex:
      /\Q.*?\E/ will match '.*?'

      Comment

      • rajiv07
        New Member
        • Jun 2007
        • 141

        #4
        Originally posted by nithinpes
        Change this line:
        Code:
        my @ee=grep(/^$file$/,@array);
        to :

        Code:
        my @ee=grep(/^\Q$file\E$/,@array);
        In your case, $file contains forward slashes which will mess up the regex(as they stand for pattern delimiters). Even using double quotes and escaping the slashes will not work.
        If you put a pattern between \Q and \E, any special meaning of the characters in pattern match will be ignored (variable substitutions will be allowed). For ex:
        /\Q.*?\E/ will match '.*?'
        Thank You Nithin,

        Why cannot we use escape the special regex char.its also working fine.Is any problem will occur when using this concept,if so please let me know and could u suggest any regexp site to refer.

        Regards

        Rajiv

        Comment

        • nithinpes
          Recognized Expert Contributor
          • Dec 2007
          • 410

          #5
          Originally posted by rajiv07
          Thank You Nithin,

          Why cannot we use escape the special regex char.its also working fine.Is any problem will occur when using this concept,if so please let me know and could u suggest any regexp site to refer.

          Regards

          Rajiv
          Sorry for the confusion and missed phrase. I mean to say: "Even using double quotes and escaping the slashes will not be a tidy work" (as it includes n number of slashes, dots & probably other characters).
          So, instead of escaping each & every special characters, using \Q and \E would be crisp and easy.

          Comment

          Working...