Solved

Perl regex ignoring [] meta characters

Posted on 2014-03-23
6
448 Views
Last Modified: 2014-03-24
Hi Experts,

I'm having trouble with a Perl regex which tests whether one string is contained in another.  In real life, the data comes from a file and a database, but the code snippet below demonstrates the issue:

    $var1 = 'A [B:2013-03] C';
    $var2 = '[B:2013-03]';
    print "Match!\n" if $var1 =~ /$var2/;

The above gives this error message:
Invalid [] range "3-0" in regex; marked by <-- HERE in m/[B:2013-0 <-- HERE 3]/ at ./sub.pl line 3.

How can I best avoid this problem and get the match to work by ignoring the meta characters?

I know I could escape the [] in $var2, like this:
    $var2 = '\[B:2013-03\]';
but I'm hoping there's a cleaner way.

Thanks.
tel2
0
Comment
Question by:tel2
  • 3
  • 3
6 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 39949629
$var1 =~ /\Q$var2\E/
or
index($var1,$var2) >= 0
0
 
LVL 12

Author Comment

by:tel2
ID: 39951740
Well said, ozo.  TIMTOWTDI.

In your first solution, I guess I could omit the '\E' in this case.

Thank you.  That's just what I was after.
tel2
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 500 total points
ID: 39951750
Or, if you are testing many $var1's with the same $var2, you may want to first do
$var2 = qr/\Q$var2/;
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Author Comment

by:tel2
ID: 39951769
For performance, ozo?
0
 
LVL 84

Expert Comment

by:ozo
ID: 39951794
yes
0
 
LVL 12

Author Closing Comment

by:tel2
ID: 39951809
Thanks.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question