7

ほとんどのstringr関数は、対応する関数の単なるラッパーstringiです。str_replace_allその一つです。stri_replace_allしかし、私のコードは、対応するstringi関数で動作しません。

キャメルケース (のサブセット) を間隔のある単語に変換するための簡単な正規表現を書いています。

なぜこれが機能するのか、私はかなり困惑しています:

str <- "thisIsCamelCase aintIt"
stringr::str_replace_all(str, 
                         pattern="(?<=[a-z])([A-Z])", 
                         replacement=" \\1")
# "this Is Camel Case ain't It"

そして、これはしません:

stri_replace_all(str, 
                 regex="(?<=[a-z])([A-Z])", 
                 replacement=" \\1")
# "this 1s 1amel 1ase ain't 1t"
4

2 に答える 2

1

以下のオプションは、どちらの場合も同じ出力を返すはずです。

pat <- "(?<=[a-z])(?=[A-Z])"
str_replace_all(str, pat, " ")
#[1] "this Is Camel Case aint It"
stri_replace_all(str, regex=pat, " ")
#[1] "this Is Camel Case aint It"

のヘルプ ページによると、置換に使用される?stri_replace_allことを示唆する例があります。$1$2

stri_replace_all_regex('123|456|789', '(\\p{N}).(\\p{N})', '$2-$1')

したがって、を置き換えると機能するはず\\1です$1

stri_replace_all(str, regex = "(?<=[a-z])([A-Z])", " $1")
#[1] "this Is Camel Case aint It"
于 2016-08-19T11:01:57.097 に答える