Mastering Regular Expression 代码介绍 1 - 重复单词处理

$/ = ".\n";   # Sets a special ``chunk-mode''; chunks end with aperiod-newline combination

 

while (<>)

{

   next unless s{# (regexstarts here)

 

         ### Need to match oneword:

         \b           # Start of word . . . .

         ( [a-z]+ )  # Grab word, filling $1 (and \1).

 

         ### Now need to allow anynumber of spaces and/or <TAGS>

         (         # Save what intervenes to $2.

             (?:   # (Non-capturing parens for grouping the alternation)

                 \s       # Whitespace (includes newline, which is good).

                |         # -or-

                 <[^>]+>  # Item like <TAG>.

             )+       # Need at least one of the above, but allowmore.

         )

 

         ### Now match the firstword again:

         (\1\b)     # \b ensures not embedded. This copy saved to$3.

 

      #(regex ends here)

    }

    # Above is the regex. The replacement string isbelow, followed by the modifiers, /i, /g, and /x

    {\e[7m$1\e[m$2\e[7m$3\e[m}igx

 

    s/^(?:[^\e]*\n)+//mg;     # Remove any unmarked lines.

    s/^/$ARGV:/mg;         # Ensure lines begin with filename.

   print;

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值