- pos SCALAR
- pos
-
Returns the offset of where the last
m//g
search left off for the variable in question ($_
is used when the variable is not specified). This offset is in characters unless the (no-longer-recommended)use bytes
pragma is in effect, in which case the offset is in bytes. Note that 0 is a valid match offset.undef
indicates that the search position is reset (usually due to match failure, but can also be because no match has yet been run on the scalar).対象の変数に対して、前回の
m//g
が終了した場所の オフセットを返します(変数が指定されなかった場合は$_
が 使われます)。 オフセットは、(もはや勧められない)use bytes
プラグマが有効の 場合(この場合はバイト単位です)を除いて、文字単位です。 0 は有効なマッチオフセットであることに注意してください。undef
は検索位置がリセットされることを意味します (通常は マッチ失敗が原因ですが、このスカラ値にまだマッチングが 行われていないためかもしれません)。pos
directly accesses the location used by the regexp engine to store the offset, so assigning topos
will change that offset, and so will also influence the\G
zero-width assertion in regular expressions. Both of these effects take place for the next match, so you can't affect the position withpos
during the current match, such as in(?{pos() = 5})
ors//pos() = 5/e
.pos
は正規表現エンジンがオフセットを保存するために使う場所を 直接アクセスするので、pos
への代入はオフセットを変更し、 そのような変更は正規表現における\G
ゼロ幅アサートにも影響を与えます。 これらの効果の両方は次のマッチングのために行われるので、(?{pos() = 5})
やs//pos() = 5/e
のように現在のマッチング中のpos
の位置には影響を与えません。Setting
pos
also resets the matched with zero-length flag, described under "Repeated Patterns Matching a Zero-length Substring" in perlre.pos
を設定すると、 "Repeated Patterns Matching a Zero-length Substring" in perlre に 記述されている、長さ 0 でマッチング フラグもリセットされます。Because a failed
m//gc
match doesn't reset the offset, the return frompos
won't change either in this case. See perlre and perlop.m//gc
マッチに失敗してもオフセットはリセットしないので、pos
からの返り値はどちらの場合も変更されません。 perlre と perlop を参照してください。