467,109 Members | 1,331 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,109 developers. It's quick & easy.

Help on Regex

rajiv07
100+
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
  • viewed: 1331
Share:
4 Replies
rajiv07
100+
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 256MB
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+
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 256MB
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.

Similar topics

8 posts views Thread by Bibe | last post: by
4 posts views Thread by H | last post: by
4 posts views Thread by henrik | last post: by
9 posts views Thread by jmchadha@gmail.com | last post: by
2 posts views Thread by Alex Maghen | last post: by
3 posts views Thread by =?Utf-8?B?TmF2ZWVu?= | last post: by
10 posts views Thread by supercrossking@gmail.com | last post: by
reply views Thread by Support Desk | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.