Might not be trimming $string2 properly, eg, spaces, \r or other characters which are hard to see.
Some things to try:
- If you had trimmed with chop, try switching to chomp.
- Use $string =~ s/^\s*|\s*$//g to remove whitespace before and after (preserves blanks within $string2)
- If you can limit what should appear in $string2, filter it with something like
$string2 =~ y/[a-zA-z0-9%,.\$]//cd (adjust as needed)