$_ = "foo\n Oops"; print /^(?=.{0,50}$).*foo/i ? "Matches!" : $_; #### /(?=^.{0,50}\z).*foo/si #### $str =~ s/(foo)/&do_stuff($str) if length $1 < 50; $1/e;