Easy Perl Regex

i need to match these with quote from a lagre string
'i am a boy'
or
"'i am a boy"

my current regex is /('|")i am a boy('|")/
but it returns matches like
'i am a boy"  < ----- first single quote, last double quote
"i am a boy" < ----- first double quote, last single quote

i don't want to do like /'i am a boy'|")i am a boy"/
does anyone know?
LVL 13
marchentAsked:
Who is Participating?
 
nedfineCommented:
$str =~ /[\"\']cat[\"\']/
0
 
nedfineCommented:
/^(\"|\')i am a boy\1/
0
 
ozoCommented:
/(["'])i am a boy\1/
0
 
marchentAuthor Commented:
i want this should work, not matching....
$str = "\'cat\"";
if($str =~ /(["'])cat\1/){
      print "YES\n";
}
else{
      print "NO\n";
}

it should match  all below.
'cat"
"cat'
'cat'
"cat"
0
 
ozoCommented:
for $str ( <DATA> ){
 if( $str =~ /["']cat["']/ ){
      print "YES\n";
 }else{
      print "NO\n";
 }
}
__DATA__
'cat"
"cat'
'cat'
"cat"
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.