• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 896
  • Last Modified:

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?
0
marchent
Asked:
marchent
  • 2
  • 2
2 Solutions
 
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
 
nedfineCommented:
$str =~ /[\"\']cat[\"\']/
0
 
ozoCommented:
for $str ( <DATA> ){
 if( $str =~ /["']cat["']/ ){
      print "YES\n";
 }else{
      print "NO\n";
 }
}
__DATA__
'cat"
"cat'
'cat'
"cat"
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now