See quotemeta (and/or the docs for \Q in (I believe) perlop's discussion on quotelikes), and read perlretut.
Update: Added parenthetical about \Q.
The cake is a lie.
The cake is a lie.
The cake is a lie.
| [reply] [d/l] [select] |
if($jpn =~ m/\Q$ipn\E/) {
| [reply] [d/l] |
Although the quotemeta and \Q\E answers are correct, you can also use \(\) like so:
if( m/(\()/ ) {
print "I matched a paren: $1\n";
}
In fact, quotemeta and \Q\E will actually transform a string usefully (so you can see what escapes to use for next time):
print "\Qhrm, do I have to escape dots (.) and question marks?\E\n";
UPDATE: I thought quotemeta was smarter than s/(\W)/\\$1/g. count me underwhelmed. I did wonder why it was escaping spaces.
| [reply] [d/l] [select] |
In fact, quote meta and \Q\E will actually transform a string usefully (so you can see what escapes to use for next time):
Well, quotemeta and \Q will simple escape any character that isn't a letter, digit or underscore. It doesn't have any knowledge of which characters are special and which aren't, except for the promise that if character that isn't a letter, digit or underscore is escaped, it will never carry a special meaning inside a regexp. I'm not quite sure what you mean by "useful" and "see what escapes to use for next time", but people often frown when commas, spaces and other 'harmless' punctuation characters are escaped in a regexp literal.
| [reply] |
quotemeta is both defined by s/(\W)/\\$1/ and by "being the function with which you escape strings in regexes", which means that in perl 5, a sequence of a backslash and a non-word-character will always literally match the non-word characters (because otherwise it would break too much existing code).
Sadly also no non-word-character can get a new meaning without a preceding backslash, because it would also break stuff.
So there remain only a few possible ways in which new features can be introduced syntactically:
- With modifiers that change the syntax
- By using one of the rare free escape sequences (\F, \T, \y, \Y)
- By constructs that are now syntactically not valid, because they don't make any sense (perhaps ({...}), previously (* ... ))
All of these don't quite help to clean up the syntax, though.
| [reply] [d/l] [select] |
Don't forget to use a \ to escape out special characters when using regex.
#!/pw/prod/svr4/bin/perl
use warnings;
use strict;
use Data::Dumper;
my $ipn= qr/\( going \) there/;
my $jpn="( going ) there";
if($jpn =~ m/$ipn/) {
print "yes";
}
else
{print "nomatch";}
Lots of good regex info if you search around Perl Monks try Perlmonks Tutorial: Regular Expressions Tutorial, the Basics (for BEGINNERS)I have also found http://www.regular-expressions.info/quickstart.html very helpful working with regex's. Lots of good examples.I found tools like http://www.txt2re.com/ or Eclipses regex tool to be helpful in debugging my regex commands.Good Luck.
-- Grey Fox
"We are grey. We stand between the darkness and the light" B5
| [reply] [d/l] |
my $ipn=qr/\( going \) there/;
or
my $ipn=qw/\( going \) there/;
but it doesn't work with
my $ipn="\( going \) there";
Thanks. | [reply] [d/l] [select] |
my $ipn="\\( going \\) there";
The \\ gets replaced by \ when the string is parsed.
Perl reduces RSI - it saves typing
| [reply] [d/l] |