蔵書の苦しみ。という以前買っていた新書が不意に出てきたので読んだ。
蔵書の苦しみ (光文社新書)
これ、kindle版もあったのか。
この種の新書は電書の方がいいかなあ。
Regular Expression(正規表現) Part12
143 デフォルトの名無しさん [sage] 2013/12/28(土) 13:39:07.55 ID: Be:
/imsxadlup の /d /l /u の使い方教えて。英語読めないの。
http://perldoc.jp/docs/perl/5.14.1/perlre.pod#47l
146 デフォルトの名無しさん [sage] 2013/12/28(土) 14:19:31.38 ID: Be:
>>143
/d
この修飾子は、パターンマッチングの際にプラットフォーム固有のロケール(ASCIIなど)を使用するように指示する。
ただし、Unicodeを使うべき理由がある場合はこの設定はそちらが優先される。
この修飾子が具体的にパターンマッチングにどのような変化をもたらすかを以下のサンプルで示した。
/l
この修飾子は、パターンマッチングの際に現在のロケール設定を使用するように指示する。
このロケール設定はコンパイル時のロケールと同じであるとは限らず、
setlocale()を用いて任意のロケール設定を用いることができる。
/u
この修飾子は、パターンマッチングの際にUnicodeのルールを使用するように指示する。
148 143 [sage] 2013/12/28(土) 15:17:11.45 ID: Be:
>>146
おお、ありがとうございます。マジで助かりました。
おかげ様でperl 5.14.1の正規表現ルールを全部覚えられました、感謝~。
149 デフォルトの名無しさん [] 2013/12/28(土) 15:18:54.48 ID: Be:
>>148
どういたしまして。
やたらとでてくる (TBT)ってなんだろうと思ったけど、
これ perldoc.jp のページだから翻訳される(予定がある)ってことで、
To Be Translate 辺りってことね。
にしたって、5.14って結構前だよねえ。
Jesse Vincent / perl-5.14.1 - search.cpan.org
2011年6月かな?
手が足らないのかしらん。
perlre - Perl 正規表現 - perldoc.jp
Character set modifiers
/d, /u, /a, and /l, available starting in 5.14, are called the character set modifiers; they affect the
character set semantics used for the regular expression. (TBT)
5.14 から使えるようになった /d /u /a /l は character set modifier と呼ばれるもので、
正規表現で使用する character set semantics に影響を及ぼします。
At any given time, exactly one of these modifiers is in effect. Once compiled, the behavior doesn't change
regardless of what rules are in effect when the regular expression is executed. And if a regular expression is
interpolated into a larger one, the original's rules continue to apply to it, and only it. (TBT)
At any given time, これらの modifier のただ一つだけが有効になります。一旦コンパイルされてしまうと、正規表現が
実行されたときに有効な rule がなんであれその振る舞いが変わることはありません。また、
ある正規表現がより大きな正規表現で interpolate された場合、
original's rules が適用され続けますし, and only it.
Note that the modifiers affect only pattern matching, and do not extend to any replacement done. For example, (TBT)
これらの modifier はパターンマッチングに対してのみ影響し、replacement には一切拡張されないことに注意してください。
たとえば
s/foo/\Ubar/l
will uppercase "bar", but the /l does not affect how the \U operates. If use locale is in effect, the
\U will use locale rules; if use feature 'unicode_strings' is in effect, it will use Unicode rules, etc. (TBT)
これは "bar" を大文字にしますが、/l は \U がどのように operate するかには影響しません。
use locale している場合には ¥U は locale rules を使用します。
use feature 'unicode_strings している場合には Unicode rules を使用します。
/l
means to use the current locale's rules (see perllocale) when pattern matching. For example, \w will match the
"word" characters of that locale, and "/i" case-insensitive matching will match according
to the locale's case folding rules. The locale used will be the one in effect at the time of execution of the
pattern match. This may not be the same as the compilation-time locale, and can differ from one match to another
if there is an intervening call of the setlocale() function. (TBT)
この modifier は current locale の rules (perllocale 参照) を使うよう指示します。たとえば \w はその locale での
"word" characters にマッチし、"/i" case-insensitive matching ではその locale の case folding rules
に従ってマッチします。実行時の locale はコンパイル時の locale とは異なる可能性があって、intervening call of the
setlocale() function が行われた場合には別のキャラクターにマッチしてしまうことがあるかもしれません。
Perl only supports single-byte locales. This means that code points above 255 are treated as Unicode no matter
what locale is in effect. Under Unicode rules, there are a few case-insensitive matches that cross the 255/256
boundary. These are disallowed under /l. For example, 0xFF does not caselessly match the character at 0x178,
LATIN CAPITAL LETTER Y WITH DIAERESIS, because 0xFF may not be LATIN SMALL LETTER Y WITH DIAERESIS in the
current locale, and Perl has no way of knowing if that character even exists in the locale, much less what code
point it is. (TBT)
Perl は single-byte locale のみをサポートしています。これはつまり、255 を越えたコードポイントは in effect な
locale に関係なく Unicode として扱われるということです。Unicode rules の下では、255/256 boundary を跨ぐような
case-insensitive なマッチが幾つか存在していますが、それらは /l によって禁止されます。たとえば 0xFF は 0x178 に
ある character とは caselessly match しません。なぜなら 0xFF はその current locale において
LATIN SMALL LETTER Y WITH DIAERESIS は使えないからです。Perl は current locale においてそういった character が
存在しているかどうかすら知る手段を持っていません。
This modifier may be specified to be the default by use locale, but see "Which character set modifier is in effect?". (TBT)
この modifier は use locale したときのデフォルトとなります。
詳細は "Which character set modifier is in effect?" を参照してください。
/u
means to use Unicode rules when pattern matching. On ASCII platforms, this means that the code points between
128 and 255 take on their Latin-1 (ISO-8859-1) meanings (which are the same as Unicode's), whereas in strict
ASCII their meanings are undefined. Thus the platform effectively becomes a Unicode platform, hence, for
example, \w will match any of the more than 100_000 word characters in Unicode. (TBT)
この modifier はパターンマッチングの際に Unicode rules を使用させます。つまり、ASCII platfotm においては
strict ASCII では meanigs が undined になっている 128 から 255 の間にある code point は Latin-1 (ISO-8859-1)
meanings として扱われます (これは Unicode のそれと同じです)。
Thus the platform effectively becomes a Unicode platform, hence, for example,
\w will match any of the more than 100_000 word characters in Unicode.
Unlike most locales, which are specific to a language and country pair, Unicode classifies all the characters
that are letters somewhere as \w. For example, your locale might not think that LATIN SMALL LETTER ETH is a
letter (unless you happen to speak Icelandic), but Unicode does. Similarly, all the characters that are decimal
digits somewhere in the world will match \d; this is hundreds, not 10, possible matches. And some of those
digits look like some of the 10 ASCII digits, but mean a different number, so a human could easily think a
number is a different quantity than it really is. For example, BENGALI DIGIT FOUR (U+09EA) looks very much
like an ASCII DIGIT EIGHT (U+0038). And, \d+, may match strings of digits that are a mixture from different
writing systems, creating a security issue. "num()" in Unicode::UCD can be used to sort this out. (TBT)
大部分の locale とは違い Unicode ではすべての character を \w のような letter と classify しています。たとえば
あなたの使っている locale では LATIN SMALL LETTER ETH を letter とはみなしていないかもしれませんが Unicode では
letter とみなします。同様に、世界中にある decimal digit すべてが \d にマッチします。つまり、マッチする可能性の
あるものが十個ではなく数百個あります。そしてそれらの中には十個の ASCII digit のどれかに見た目が似ているけれども
意味が異なっているので人が見たときに実際の数とは違った数と容易に思い込んでしまうようなものが存在しています。
一例を挙げると、BENGALI DIGIT FOUR (U+09EA) は ASCII DIGIT EIGHT (U+0038) に見た目が非常に似ています。
そして \d は a mixture from different writing systems な strings of digits にマッチしてしまうために
sceurity issue を作ってしまいます。
Unicode::UCD にある "num()" はソートするのに使えます。
Also, case-insensitive matching works on the full set of Unicode characters. The KELVIN SIGN, for example
matches the letters "k" and "K"; and LATIN SMALL LIGATURE FF matches the sequence "ff",
which, if you're not prepared, might make it look like a hexadecimal constant, presenting another potential
security issue. See http://unicode.org/reports/tr36 for a detailed discussion of Unicode security issues. (TBT)
また、case-insensitive マッチングは Unicode における character 全体に作用します。たとえば KELVIN SIGN は
"k" や "K" という letter にマッチし、LATIN SMALL LIGATURE FF は "ff" という並びに
マッチします。
which, if you're not prepared,
might make it look like a hexadecimal constant, presenting another potential security issue.
See http://unicode.org/reports/tr36 for a detailed discussion of Unicode security issues. (TBT)
On the EBCDIC platforms that Perl handles, the native character set is equivalent to Latin-1. Thus this modifier
changes behavior only when the "/i" modifier is also specified, and it turns out it affects only two
characters, giving them full Unicode semantics: the MICRO SIGN will match the Greek capital and small letters MU,
otherwise not; and the LATIN CAPITAL LETTER SHARP S will match any of SS, Ss, sS, and ss, otherwise not. (TBT)
This modifier may be specified to be the default by use feature 'unicode_strings, but see "Which character
set modifier is in effect?". (TBT)
/a
is the same as /u, except that \d, \s, \w, and the Posix character classes are restricted to matching in the
ASCII range only. That is, with this modifier, \d always means precisely the digits "0" to "9";
\s means the five characters [ \f\n\r\t]; \w means the 63 characters [A-Za-z0-9_]; and likewise, all the Posix
classes such as [[:print:]] match only the appropriate ASCII-range characters. (TBT)
/u とほぼ同じですが、\d、\s、\w および POSIX character class のマッチ対象は ASCII の範囲に制限されます。
つまり、この modifier をつけることによって \d は常に "0" から "9" までの digit を意味するようになり、
\s は [ \f\n\r\t] の五つの character を、\w は [A-Za-z0-9_]; の63個の character を意味するようになります。
また、[[:print:]] のような POSIX class についてもすべて ASCII-range characters だけがマッチの対象になります。
This modifier is useful for people who only incidentally use Unicode. With it, one can write \d with confidence
that it will only match ASCII characters, and should the need arise to match beyond ASCII, you can use \p{Digit},
or \p{Word} for \w. There are similar \p{...} constructs that can match white space and Posix classes beyond
ASCII. See "POSIX Character Classes" in perlrecharclass. (TBT)
この modifier は incidentally にのみ Unicode を使う人にとって有用です。
With it, one can write \d with confidence
that it will only match ASCII characters,
and should the need arise to match beyond ASCII, you can use \p{Digit},
or \p{Word} for \w.
There are similar \p{...} constructs that can match white space and Posix classes beyond ASCII.
perlrecharclass にある "POSIX Character Classes" を参照してください。
As you would expect, this modifier causes, for example, \D to mean the same thing as [^0-9]; in fact, all
non-ASCII characters match \D, \S, and \W. \b still means to match at the boundary between \w and \W, using the
/a definitions of them (similarly for \B). (TBT)
おそらく期待しているように、この modifier はたとえば \D を [^0-9]; と等価な意味にします。
つまり、すべての non-ASCII character が \D、\S、\W にマッチします。
\b still means to match at the boundary between \w and \W, using the /a definitions of them (similarly for \B).
Otherwise, /a behaves like the /u modifier, in that case-insensitive matching uses Unicode semantics; for
example, "k" will match the Unicode \N{KELVIN SIGN} under /i matching, and code points in the Latin1
range, above ASCII will have Unicode rules when it comes to case-insensitive matching. (TBT)
それ以外については /a は /u と同じように、Unicode semantics を用いた case-insentive マッチングを行います。
たとえば "k" は /i マッチングの場合には Unicode \N{KELVIN SIGN} にマッチしますし、Latin1 の範囲にあって
above ASCII な コードポイントは case-insentive マッチングが必要になった場合には Unicode rule が使われます。
To forbid ASCII/non-ASCII matches (like "k" with \N{KELVIN SIGN}), specify the "a" twice, for
example /aai or /aia (TBT)
("k" with \N{KELVIN SIGN} のような) ASCII/non-ASCII マッチを禁じるには /aai や /aia のように "a"
を二回指定します
To reiterate, this modifier provides protection for applications that don't wish to be exposed to all of Unicode.
Specifying it twice gives added protection. (TBT)
This modifier may be specified to be the default by use re '/a' or use re '/aa', but see "Which character
set modifier is in effect?". (TBT)
/d
This modifier means to use the "Default" native rules of the platform except when there is cause to
use Unicode rules instead, as follows: (TBT)
この modifier はその platform の "Default" native rules を使うよう指示しますが、
以下のような Unicode rules を使う理由のある場合は例外です。
ターゲット文字列が UTF-8 でエンコードされている; または
パターンが UTF-8 でエンコードされている; または
パターンが、(\x{100} のような形で)255 を超える符号位置に明示的に言及している; または
パターンが Unicode 名 (\N{...}) を使っている; または
パターンが Unicode 特性 (\p{...}) を使っている
Another mnemonic for this modifier is "Depends", as the rules actually used depend on various things,
and as a result you can get unexpected results. See "The "Unicode Bug"" in perlunicode. (TBT)
もう一つの mnemonic for this modifier は、実際に使用している rules に様々な状況で依存しているので
期待したのとは異なる結果が得られてしまう可能性があるということです。
On ASCII platforms, the native rules are ASCII, and on EBCDIC platforms (at least the ones that Perl handles),
they are Latin-1. (TBT)
Here are some examples of how that works on an ASCII platform: (TBT)
$str = "\xDF"; # $str is not in UTF-8 format.
$str =~ /^\w/; # No match, as $str isn't in UTF-8 format.
$str .= "\x{0e0b}"; # Now $str is in UTF-8 format.
$str =~ /^\w/; # Match! $str is now in UTF-8 format.
chop $str;
$str =~ /^\w/; # Still a match! $str remains in UTF-8 format.
「やっつけ訳」なので参考程度に。
んで、この先も見てて気になったのがあって
perlre - Perl 正規表現 - perldoc.jp
文字クラス及び他の特殊なエスケープ
さらに、Perl は以下のものを定義します:
Sequence Note Description
(略)
\g1 [5] 指定したまたは前のグループへの後方参照
\g{-1} [5] 数値は相対的に前のグループを示す負の値にもできます、また
任意で安全にパースするために波かっこで括ることもできます
\g{name} [5] 名前指定の後方参照
\k<name> [5] 名前指定の後方参照
\g1 の説明がよくわからん。どゆこと?
で、こういうときは原文に当たれ。と。
perlre - perldoc.perl.org
\g1 [5] Backreference to a specific or previous group,
\g{-1} [5] The number may be negative indicating a relative previous group and may optionally be wrapped in
curly brackets for safer parsing.
あー、\g1 の直後の記述って , で終わってるから、
これを . 扱いしてここで説明文が終わってるとみなすとおかしくなるっぽい?
[5] の参照先も併せて考えると
perlre - perldoc.perl.org
Capture groups
The bracketing construct ( ... ) creates capture groups (also referred to as capture buffers). To refer to the
current contents of a group later on, within the same pattern, use \g1 (or \g{1} ) for the first, \g2 (or \g{2})
for the second, and so on. This is called a backreference. There is no limit to the number of captured
substrings that you may use. Groups are numbered with the leftmost open parenthesis being number 1, etc. If a
group did not match, the associated backreference won't match either. (This can happen if the group is optional,
or in a different branch of an alternation.) You can omit the "g" , and write "\1" , etc,
but there are some issues with this form, described below.
You can also refer to capture groups relatively, by using a negative number, so that \g-1 and \g{-1} both refer
to the immediately preceding capture group, and \g-2 and \g{-2} both refer to the group before it. For example:
ん、 \g1 \g2 のようなのが
Backreference to a specific group
で、
\g-1 \g-2 のようなのが
Backreference to a previous group
ということか(で、数字部分を {} でくくっても良し。と)。
awkの、コマンドライン上での変数代入について
@node Other Arguments
@section Other Command-Line Arguments
@cindex command line, arguments
@cindex arguments, command-line
Any additional arguments on the command line are normally treated as
input files to be processed in the order specified. However, an
argument that has the form @code{@var{var}=@var{value}}, assigns
the value @var{value} to the variable @var{var}---it does not specify a
file at all.
(See
@ref{Assignment Options}.)
@cindex @command{gawk}, @code{ARGIND} variable in
@cindex @code{ARGIND} variable, command-line arguments
@cindex @code{ARGC}/@code{ARGV} variables, command-line arguments
All these arguments are made available to your @command{awk} program in the
@code{ARGV} array (@pxref{Built-in Variables}). Command-line options
and the program text (if present) are omitted from @code{ARGV}.
All other arguments, including variable assignments, are
included. As each element of @code{ARGV} is processed, @command{gawk}
sets the variable @code{ARGIND} to the index in @code{ARGV} of the
current element.
@cindex input files, variable assignments and
The distinction between @value{FN} arguments and variable-assignment
arguments is made when @command{awk} is about to open the next input file.
At that point in execution, it checks the @value{FN} to see whether
it is really a variable assignment; if so, @command{awk} sets the variable
instead of reading a file.
Therefore, the variables actually receive the given values after all
previously specified files have been read. In particular, the values of
variables assigned in this fashion are @emph{not} available inside a
@code{BEGIN} rule
(@pxref{BEGIN/END}),
because such rules are run before @command{awk} begins scanning the argument list.
@cindex dark corner, escape sequences
The variable values given on the command line are processed for escape
sequences (@pxref{Escape Sequences}).
@value{DARKCORNER}
In some earlier implementations of @command{awk}, when a variable assignment
occurred before any @value{FN}s, the assignment would happen @emph{before}
the @code{BEGIN} rule was executed. @command{awk}'s behavior was thus
inconsistent; some command-line assignments were available inside the
@code{BEGIN} rule, while others were not. Unfortunately,
some applications came to depend
upon this ``feature.'' When @command{awk} was changed to be more consistent,
the @option{-v} option was added to accommodate applications that depended
upon the old behavior.
The variable assignment feature is most useful for assigning to variables
such as @code{RS}, @code{OFS}, and @code{ORS}, which control input and
output formats before scanning the @value{DF}s. It is also useful for
controlling state if multiple passes are needed over a @value{DF}. For
example:
@cindex files, multiple passes over
@example
awk 'pass == 1 @{ @var{pass 1 stuff} @}
pass == 2 @{ @var{pass 2 stuff} @}' pass=1 mydata pass=2 mydata
@end example
Given the variable assignment feature, the @option{-F} option for setting
the value of @code{FS} is not
strictly necessary. It remains for historical compatibility.
一日目も興味あるサークルがあるにはあるんだけど見送りかなあ
(二日目も微妙…)