|
|
if ( "this is a pen." =~ /is/ ) {
print "HIT!";
}
$target = "this is a pen.";
if ( $target =~ /is/ ) {
print "HIT!";
}
| |
|
|
$target = "is";
if ( "this is a pen." =~ /$target/ ) {
print "HIT!";
}
| |
|
|
$_ = "this is a pen.";
$target = "is";
if ( /$target/ ) {
print "HIT!";
}
| |
|
|
$_ = "this is a pen.";
$target = "is";
if ( /$target/ ) {
print length($`);
}
または
print index( $_, $target );
検索の開始位置を省略した index 関数は、-1 を返すので 文字列検索の条件としても使用できる
| |
|
|
|
|
# **************************************
# 部分文字列の取り出し
# **************************************
$target = "this is a pen.";
print substr( $target, 5, 2 );
| |
|
|
# **************************************
# 文字列の部分に文字列を代入 ( 文字列置換 )
# **************************************
substr( $target, 5, 2 ) = "was";
print $target;
| |
|
|
|
|
@str = split( /,i/, "this,is,a,pen." );
foreach( @str ) {
print "$_ ";
}
| |
|
|
@str = split( /[,i]/, "this,is,a,pen." );
foreach( @str ) {
print "$_ ";
}
| |
|
|
@str = split( /,/, "this,is,a,pen." );
foreach( @str ) {
print "$_ ";
}
| |
|
|
($this,$is,$str,$pen) = split( /,/, "this,is,a,pen." );
print $str;
| |
|
|
|
|
@str = ("this", "is", "a", "pen." );
$bun = join( "/", @str );
print $bun;
| |
|
|
$bun = join( " ", ( "this", "is", "a", "pen." ) );
print $bun;
| |
|
|
|
|
$target = ""this is a pen. that is a pen too.";
$target =~ s/ is/ was/g;
print $target;
| |
|
|
|