use strict;
my $str = 'I have a string like get(3,"No MATCH",obj). I want to check if the word MATCH is enclosed within quotes (either single quote or double quote). Here MATCH is enclosed within quotes, although not exactly as "MATCH", it is still a part of the text contained in quotes as "No MATCH".
I want to write a function which takes the word (MATCH) as argument and returns true if it is contained within quotes or false if not.
Following are some other input strings which needs to be checked by this function:
* get(1101,"MATCH",obj) --> return true since MATCH is within quotes
* get(255,\'NO MATCH\',obj) --> return true since MATCH is a part of text contained within quotes
* get(1111,"" , MATCH) ---> return false since MATCH is not contained within quotes';
my $regex = qr/['"]([ \w]+)?MATCH([ \w]+)?["']/mp;
if ( $str =~ /$regex/g ) {
print "Whole match is ${^MATCH} and its start/end positions can be obtained via \$-[0] and \$+[0]\n";
# print "Capture Group 1 is $1 and its start/end positions can be obtained via \$-[1] and \$+[1]\n";
# print "Capture Group 2 is $2 ... and so on\n";
}
# ${^POSTMATCH} and ${^PREMATCH} are also available with the use of '/p'
# Named capture groups can be called via $+{name}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Perl, please visit: http://perldoc.perl.org/perlre.html