I have a file that I want to read in using the File::Slurp module then search that file for a specific line. I know Perl has regular expressions but the strings I'm searching for are user-generated so don't want to have to worry about escaping everything. I could write a foreach loop to do this but was wondering if there's a function in Perl that will do this for me? Everything I see online about finding text in Perl uses regular expressions.
You can just use a regular expression. You don't have to worry about escaping everything, Perl has the quotemeta function for that (or alternatively, "\\Q\\E").
Like this?
use List::Util qw<first>;
my $line
= first { index( $_, $something_users_are_looking_for ) > -1 }
<$file>
;
And if you want 'em all.
my @lines
= grep { index( $_, $something_users_are_looking_for ) > -1 }
<$file>
;
你能用q()来使用字符串文字吗?
print(q(This string can contain regex: (\n\t*\n)(\n)));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.