请解释这个Perl正则表达式

Che*_*eso 1 regex string perl

    $rowfetch =~ s/['-]//g; #All chars inside the [ ] will be filtered out.
    $rowfetch =~ m/(\w+), ?(.)/;
    printf $fh lc($2.$1);
Run Code Online (Sandbox Code Playgroud)

我昨天得到了帮助建立这个正则表达式,但我不完全理解它.

它的名字像Parisi,Kenneth,并打印出kparisi

Knowns:
s/= substitute
m/= match


我试图寻找其余的但却找不到任何真正有助于解释它的东西.

我也不明白=〜应该如何评估为true还是false,但在这种情况下,它正在修改字符串.

Ed *_*ess 22

我发现该YAPE::Regex::Explain模块非常有用 -

C:\>perl -e "use YAPE::Regex::Explain;print YAPE::Regex::Explain->new(qr/['-])->explain;"
The regular expression:

(?-imsx:['-])

matches as follows:

NODE                     EXPLANATION
----------------------------------------------------------------------
(?-imsx:                 group, but do not capture (case-sensitive)
                         (with ^ and $ matching normally) (with . not
                         matching \n) (matching whitespace and #
                         normally):
----------------------------------------------------------------------
  ['-]                     any character of: ''', '-'
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------



C:\>perl -e "use YAPE::Regex::Explain; print YAPE::Regex::Explain->new(qr/(\w+), ?(.)/)->explain;"
The regular expression:

(?-imsx:(\w+), ?(.))

matches as follows:

NODE                     EXPLANATION
----------------------------------------------------------------------
(?-imsx:                 group, but do not capture (case-sensitive)
                         (with ^ and $ matching normally) (with . not
                         matching \n) (matching whitespace and #
                         normally):
----------------------------------------------------------------------
  (                        group and capture to \1:
----------------------------------------------------------------------
    \w+                      word characters (a-z, A-Z, 0-9, _) (1 or
                             more times (matching the most amount
                             possible))
----------------------------------------------------------------------
  )                        end of \1
----------------------------------------------------------------------
  ,                        ','
----------------------------------------------------------------------
   ?                       ' ' (optional (matching the most amount
                           possible))
----------------------------------------------------------------------
  (                        group and capture to \2:
----------------------------------------------------------------------
    .                        any character except \n
----------------------------------------------------------------------
  )                        end of \2
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------

C:\>
Run Code Online (Sandbox Code Playgroud)


tva*_*son 10

我将这些备忘单中的一张固定在我的立方体墙上以备这种情况.谷歌regular expression cheat sheet寻找其他人.

添加到您已经知道的内容:

  g -- search globally throughout the string
  + -- match at least one, but as many as possible
  ? -- match 0 or 1
  . -- match any character
 () -- group these together
  , -- a plain comma, no special meaning
 [] -- match any character inside the brackets
 \w -- match any word character
Run Code Online (Sandbox Code Playgroud)

神奇的是分组 - 匹配表达式使用组并将它们放入变量$ 1和$ 2.在这种情况下,$ 1匹配逗号前的单词,$ 2匹配逗号后面的空白后面的第一个字符.