$B%P%C%/%9%i%C%7%e$r4^$`(Bsub/gsub$B$N5sF0(B

e$B$O$8$a$^$7$F!#J!ED$H?=$7$^$9!#e(B

sub/gsube$B$N5sF0$G!“e(B
'e$B!!”!!e(B\’
e$B$H$$$&CV49$r$7$h$&$H$7$?>l9g$N5sF0$K$D$$$F$G$9!#2<5-$NF0:n$O0U?^E
$J$b$N$G$7$g$&$+!)e(B

e$B!J%7%s%0%k%/%)!<%H!K0J30$b!“5/$3$kJ8;z$H5/$3$i$J$$J8;z$,$”$k$h$&$G$9!#e(B
e$BLVMeE*$K;n$7$?$o$1$G$O$J$$$G$9$,!"e(B’&
e$B$J$I$G$O5/$3$j!“e(B$#e$B$J$I$G$O5/$3$j$^$;$s!#e(B
sub/gsub, 1.8/1.9e$B!”$NA4$F$G6&DL$N$h$&$G$9!#e(B

$ ./ruby -v
ruby 1.9.0 (2008-05-16 revision 0) [i686-linux]

$ ./ruby
p “a”.sub(/a/, “\a”)
p “’”.sub(/’/, “\’”) # <–e$B!)e(B
p “‘foo’”.sub(/’/, “\’”) # <-- ?
p “‘foo’”.sub(/’/, “\’”) # <-- ?

p “a”.sub(/a/) { |m| “\a” }
p “’”.sub(/’/) { |m| “\’” } #
e$B%V%m%C%/$N>l9g$O0c$&5sF0e(B(e$B$3$A$i$,K\Mh!&!&!&e(B??e$B!Ke(B
p “‘foo’”.sub(/’/) { |m| “\’” }
p “‘foo’”.sub(/’/) { |m| “\’” }

==>

“\a”
“”
“foo’foo’”
“foo’foo’”
“\a”
“\’”
“\‘foo’”
“\‘foo’”

$ ./ruby -v
ruby 1.8.7 (2008-05-16 patchlevel 5000) [i686-linux]

p “a”.sub(/a/, “\a”)
p “’”.sub(/’/, “\’”)
p “‘foo’”.sub(/’/, “\’”)
p “‘foo’”.sub(/’/, “\’”)

p “a”.sub(/a/) { |m| “\a” }
p “’”.sub(/’/) { |m| “\’” }
p “‘foo’”.sub(/’/) { |m| “\’” }
p “‘foo’”.sub(/’/) { |m| “\’” }

==>

“\a”
“”
“foo’foo’”
“foo’foo’”
“\a”
“\’”
“\‘foo’”
“\‘foo’”

keisuke fukuda e$B$5$s$O=q$-$^$7$?e(B:

e$B$O$8$a$^$7$F!#J!ED$H?=$7$^$9!#e(B

sub/gsube$B$N5sF0$G!“e(B
'e$B!!”!!e(B\’
e$B$H$$$&CV49$r$7$h$&$H$7$?>l9g$N5sF0$K$D$$$F$G$9!#2<5-$NF0:n$O0U?^E
$J$b$N$G$7$g$&$+!)e(B

e$B$O$$!#e(B

e$BJ!ED$G$9!#e(B

e$B$k$S$-$A$5$s!“KNIt$5$s!”$"$j$,$H$&$4$6$$$^$7$?!#e(B
e$B$b$C$H$h$/3NG’$7$F$+$iN.$9!Je(Bruby-liste$B$K!K$h$&$K$7$^$9!#e(B

e$B$*A{$,$;$7$^$7$?!#e(B

From: “keisuke fukuda” [email protected]
Subject: [ruby-dev:34727]
e$B%P%C%/%9%i%C%7%e$r4^$`e(Bsub/gsube$B$N5sF0e(B
Date: Fri, 16 May 2008 20:38:44 +0900

e$B$k$S$-$A$G$9!#e(B

ruby-liste$B8~$-$+$J!#e(B

sub/gsube$B$N5sF0$G!“e(B
'e$B!!”!!e(B\’
e$B$H$$$&CV49$r$7$h$&$H$7$?>l9g$N5sF0$K$D$$$F$G$9!#2<5-$NF0:n$O0U?^E
$J$b$N$G$7$g$&$+!)e(B
’ e$B!J%7%s%0%k%/%)!<%H!K0J30$b!“5/$3$kJ8;z$H5/$3$i$J$$J8;z$,$”$k$h$&$G$9!#e(B
e$BLVMeE*$K;n$7$?$o$1$G$O$J$$$G$9$,!"e(B’& e$B$J$I$G$O5/$3$j!"e(B$#e$B$J$I$G$O5/$3$j$^$;$s!#e(B

e$B0U?^$7$?;EMM$G$9!#e(B

— gsub(pattern, replace) -> String

e$BJ8;zNsCf$Ge(B pattern e$B$K%^%C%A$9$kItJ,A4$F$re(B
e$BJ8;zNse(B replace e$B$GCV$-49$($?J8;[email protected]@.$7JV$7$^$9!#e(B

Ruby 1.6 e$B0JA0$G$Oe(B pattern e$B$,J8;zNs$N>l9g!“e(B
e$B$=$NJ8;[email protected],I=8=$K%3%s%Q%$%k$7$F$$$^$7$?!#e(B
e$B$7$+$7e(B Ruby 1.8
e$B0J9_$G$O!”$=$NJ8;zNs$=$N$b$N$,%Q%?!<%s$K$J$j$^$9!#e(B

e$BCV49J8;zNse(B replace e$BCf$Ne(B & e$B$He(B \0
e$B$O%^%C%A$7$?ItJ,J8;zNs$K!"e(B
\1 … \9 e$B$Oe(B n e$BHVL$N3g8L$NFbMF$KCV$-49$($i$l$^$9!#e(B
e$BCV49J8;zNsFb$G$Oe(B `e$B!"e(B’e$B!"e(B+ e$B$b;H$($^$9!#e(B
e$B$3$l$i$Oe(B [[m:$`]]e$B!"e(B[[m:$’]]e$B!"e(B[[m:$+]]
e$B$KBP1~$7$^$9!#e(B

— $’

e$B8=:_$N%9%3!<%W$G:[email protected]$7$?@55,I=8=$N%Q%?!<%s%^%C%A$G%^%C%A$7$?e(B
e$BItJ,$h$j8e$m$NJ8;zNs$G$9!#e(B

e$B:G8e$N%^%C%A$,<:GT$7$F$$$?>l9g$K$Oe(B nile$B!#e(B

[[m:Regexp.last_match]].post_match e$B$HF1$8$G$9!#e(B

e$B$3$NJQ?t$O%m!<%+%k%9%3!<%W$+$D%9%l%C%I%m!<%+%k$G$9!#e(B

================

*1

“‘foo’”.sub(/’/, “\’”) # => “foo’foo’”

*2

“‘foo’”.sub(/’/, %q!’!) # => “foo’foo’”

*3

“‘foo’”.sub(/’/) { |m| Regexp.last_match.post_match } # => “foo’foo’”

1e$B$rEy2A$J$b$N$K=q$-49$($k$He(B2e$B$K$J$j$^$9!#e(B
e$B$G!"CV49J8;zNs$,!Ve(B’e$B!W$J$N$G8e$K%^%C%A$7$?J8;zNs!Ve(Bfoo’e$B!W$KCV$-49$o$k$N$G$9!#e(B
e$B%V%m%C%/5-K!$K$9$k$He(B*3e$B$H$bEy2A$G$9!#e(B

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs