3
#!/usr/bin/perl
@lines = `perldoc -u -f atan2`;
foreach (@lines) {
  s/\w<([^>]+)>/\U$1/g;
  print;
}

式はどのように機能しs/\w<([^>]+)>/\U$1/g;ますか?

4

3 に答える 3

4

これは、それが何をしているのかを把握するための別のオプションです。CPANのモジュールYAPE::Regex::Explainを使用します。

この方法で使用します (これは、検索と置換の一致部分にすぎません)。

use strict;
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):
----------------------------------------------------------------------
  \w                       word characters (a-z, A-Z, 0-9, _)
----------------------------------------------------------------------
  <                        '<'
----------------------------------------------------------------------
  (                        group and capture to \1:
----------------------------------------------------------------------
    [^>]+                    any character except: '>' (1 or more
                             times (matching the most amount
                             possible))
----------------------------------------------------------------------
  )                        end of \1
----------------------------------------------------------------------
  >                        '>'
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------

式の置換部分は、「\1 へのグループとキャプチャ」と「\1 の終わり」の間で以前に行われた一致を大文字に変換する必要があることを示しています。

于 2013-01-08T11:00:44.317 に答える
4

置換はこれを行います:

s/             
    \w<         # look for a single alphanumeric character followed by <
    ([^>]+)     # capture one or more characters that are not <
    >           # followed by a >
/               ### replace with
   \U           # change following text to uppercase
   $1           # the captured string from above
/gx             # /g means do this as many times as possible per line

修飾子を追加して/x、正規表現を視覚化できるようにしました。の後の文字で[^>]示されるように、文字クラスは否定されます。これは、「」以外の任意の文字を意味します。^[>

たとえば、perldoc コマンドからの出力では

X<atan2> X<arctangent> X<tan> X<tangent>

に変更されます

ATAN2 ARCTANGENT TAN TANGENT
于 2013-01-08T06:41:22.237 に答える
0

perl ループは次のようになります。

foreach $item (@array)
{
   # Code in here. ($item takes a new value from array each iteration)
}

しかし、perl ではほとんどどこでも変数を省略できます。
これを行うと、特別な変数$_が使用されます。

だからあなたの場合:

foreach (@lines) 
{
}

以下とまったく同じです。

foreach $_ (@lines) 
{
}

本文内に次のコードを挿入します。

s/\w<([^>]+)>/\U$1/g;

同じことが起こっています。あなたは実際に変数に取り組んでいます。また、変数を指定しない場合、perl はデフォルトで$_.

したがって、次と同等です。

$_ =~ s/\w<([^>]+)>/\U$1/g;

2 つを組み合わせる:

foreach (@lines) {
  s/\w<([^>]+)>/\U$1/g;
  print;
}

も同等です:

foreach $item (@lines)
{
    $item =~ s/\w<([^>]+)>/\U$1/g;
    print $item;
}

読みやすさのためだけに使用$itemします。内部的には を意味し$_ます。

多くの perl コードがこのタイプのショートカットを使用しています。個人的には、読みにくくなっていると思います (経験豊富な perl プログラマーにとっても (perl が読みにくいという評判を得た理由の 1 つです))。結果として、私は常に変数の使用について明確にしようとしています (しかし、これ (私の使用法) は典型的な perl の使用法ではありません)。

于 2013-01-08T06:55:57.073 に答える