regex - How to display the exact search keyword in perl -
i using regex searching directory list of keywords. here current code:
if (/$search/i) { printf $out "%s\t%s\n",$file::find::name,$1; }
in above code $1
giving keyword phrase. want entire keyword.
example: searching "sweep"
current output: c:\ac\acfrd\file.sql sweep
. file contains word "sweep_id", , want output "sweep_id", not "sweep".
try regex:
/\b(\w*$search\w*)\b/i
it captures search term , optional adjacent word-symbols (\w
- letters, digits, etc.) $1
. captured character sequence surrounded word-boundaries (\b
- punctuation, whitespace, string beginning or ending).
the regex above allows additional word-symbols both before , after search term. if want allow additional symbols after search term (as in example), remove first \w*
:
/\b($search\w*)\b/i
if not want rely on perl's definition of "word symbols", replace \w
own character class, e.g. (only allow letters , underscores):
/\b([_a-z]*$search[_a-z]*)\b/i