Regex to match whole word with a particular definition of a word

后端 未结 3 1394
故里飘歌
故里飘歌 2020-12-07 05:40

I am doing a file search and replace for occurrences of specific words in perl. I\'m not usually much of a perl or regex user. I have searched for other regex questions here

3条回答
  •  星月不相逢
    2020-12-07 06:13

    Use the \b anchor to match only on a word boundary:

    s/\bcat\b/cat_tastic/g
    

    Although Perl has a slightly different definition of what a "word" is. Reading the perlre reference guide a couple of times might help you understand regexps a bit better.

    Running perl -pi -e "YOUR_REGEXP" in a terminal and entering in lines of text can help you understand and debug what a particular regexp is doing.

提交回复
热议问题