Solved

Perl regex ignoring [] meta characters

Posted on 2014-03-23
6
440 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 11

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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 11

Author Comment

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

Expert Comment

by:ozo
ID: 39951794
yes
0
 
LVL 11

Author Closing Comment

by:tel2
ID: 39951809
Thanks.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

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 …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now