By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,934 Members | 1,685 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,934 IT Pros & Developers. It's quick & easy.

Help on Regex

rajiv07
100+
P: 141
Hi to all,

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

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. my @array=('3242342','fdsfsd4432');
  3.  
  4. my $file='D:/HTTP/sample/SAREGAMA/HINDI/01/INH100155170 O HANSINI(ZEHREELA INSAAN.wma';
  5.  
  6. my @ee=grep(/^$file$/,@array);
  7.  
  8. print @ee;

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\INDIAM~1.COM\LOCALS~1\Temp\locB8.tmp line 9.

Please Help on this.

regards Rajiv.
Jun 2 '08 #1
Share this Question
Share on Google+
4 Replies


rajiv07
100+
P: 141
I got the point now if the string contain regex special character like $,^ ) we have to escape the character.

Regards
Rajiv
Jun 2 '08 #2

nithinpes
Expert 100+
P: 410
Hi to all,

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

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. my @array=('3242342','fdsfsd4432');
  3.  
  4. my $file='D:/HTTP/sample/SAREGAMA/HINDI/01/INH100155170 O HANSINI(ZEHREELA INSAAN.wma';
  5.  
  6. my @ee=grep(/^$file$/,@array);
  7.  
  8. print @ee;

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\INDIAM~1.COM\LOCALS~1\Temp\locB8.tmp line 9.

Please Help on this.

regards Rajiv.
Change this line:
Expand|Select|Wrap|Line Numbers
  1. my @ee=grep(/^$file$/,@array);
  2.  
to :

Expand|Select|Wrap|Line Numbers
  1. my @ee=grep(/^\Q$file\E$/,@array);
  2.  
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 '.*?'
Jun 2 '08 #3

rajiv07
100+
P: 141
Change this line:
Expand|Select|Wrap|Line Numbers
  1. my @ee=grep(/^$file$/,@array);
  2.  
to :

Expand|Select|Wrap|Line Numbers
  1. my @ee=grep(/^\Q$file\E$/,@array);
  2.  
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
Jun 2 '08 #4

nithinpes
Expert 100+
P: 410
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.
Jun 2 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.