2017-11-30 10 views
1

私が保持したいものを囲むテキストブロックを削除しようとしています。だから、テキストが長くなる可能性があるので、変数を割り当てたいと思っていました。これは私がやろうとしていることの一例です。あなたが探しているものに、私は、これは一つの解決策だと思いますgsubfn |置換の変数を使用してテキストを置換する

Text<-'This is an example text [] test' 
topheader<-'This' 
bottomheader<-'test' 


gsubfn(".", list(topheader = "", bottomheader = ""), Text) 
[1] "This is an example text [] test" 


Goal: "is an example text []" 
+0

あなたは削除したい、何を保存したいですか? – AkselA

+0

"this"と "test"を削除します。すべてを真ん中に保つ –

+1

'' gsub( "This | test"、 ""、Text) 'で十分でしょうか?または 'gsub(" \\ bThis \\ b | \\ btest \\ b "、" "、Text)'?大文字小文字を区別しません? – AkselA

答えて

0

1)gsubfnを使用してを使用して

Test <- 'This is an example text testing [] test' 

top <- "This" 
bottom <- "test" 

arg <- c(top, bottom) 
arg <- paste(arg, collapse="|") 
arg <- gsub("(\\w+)", "\\\\b\\1\\\\b", arg) 

Test.c <- gsub(arg, "", Test) 
Test.c <- gsub("[ ]+", " ", Test.c) 
Test.c <- gsub("^[[:space:]]|[[:space:]]$", "", Test.c) 
Test.c 
# "is an example text []" 

それともいくつかの問題がここにあります

  • の正規表現gsubfn(およびgsub)は、処理する文字列と一致する必要がありますが、ドットは1文字のみと一致するため、4文字の文字列であるThisまたはtestには一致しません。代わりに"\\w+"を使用してください。

  • list(a = x)aは、変数ではなく定数でなければなりません。名前を明示的に書き出すか、変数に変数が含まれている場合は代わりにsetNamesを使用してください。

    library(gsubfn) 
    
    trimws(gsubfn("\\w+", list(This = "", text = ""), Text)) 
    ## [1] "is an example [] test" 
    

    またはヘッダー変数の観点から:これはtopheaderとbottomheaderの発生を置き換えること

    L <- setNames(list("", ""), c(topheader, bottomheader)) 
    trimws(gsubfn("\\w+", L, Text)) 
    ## [1] "is an example [] test" 
    

    注したがって、問題のコードを修正する

開始時と終了時のものだけでなく、しかし、これはあなたのコードに最も近い可能性が高いと思われます。

2)サブ別の可能性があり、この単純な

sub("^This (.*) text$", "\\1", Text) 
[1] "is an example [] test" 

やヘッダ変数の観点でsub

pat <- sprintf("^%s (.*) %s$", topheader, bottomheader) 
sub(pat, "\\1", Text) 
## [1] "is an example [] test" 

更新:固定(1)何

0

[テキストを削除しません]:

# Your data: 
Text<-'This is an example text [] test' 
topheader<-'This' 
bottomheader<-'test' 

# A possible solution fn 
gsubfn <- function(text, th, bh, th.replace="", bh.replace="") { 
    answer <- gsub(text, 
       pattern=paste0(th," (.*) ",bh), 
       replacement=paste0(th.replace,"\\1",bh.replace) 
       ) 
    return(answer) 
    } 

# Your req'd answer 
gsubfn(text=Text,th=topheader,bh=bottomheader) 

# Another example 
gsubfn(text=Text,th=topheader,bh=bottomheader,th.replace="@@@ ",bh.replace=" ###") 
+0

gsubがあなたの 'ヘッダーと一致させようとしているときにマッチする' Text 'のパターンを注意することが1つの注意です。そのような場合には、おそらくパターン引き数の先頭と末尾にそれぞれ "^"と "$"を追加すると、これはもう少し堅牢になります。 – Nate

0

あなただけの正規表現文字列に検索語を折りたたむことができます。 magrittrパイプ

library(magrittr) 

c(top, bottom) %>% 
paste(collapse="|") %>% 
gsub("(\\w+)", "\\\\b\\1\\\\b", .) %>% 
gsub(., "", Test) %>% 
gsub("[ ]+", " ", .) %>% 
gsub("^[[:space:]]|[[:space:]]$", "", .) -> Test.c 
Test.c 
# "is an example text []" 

またはループ

Test.c <- Test 
words <- c(top, bottom) 
for (i in words) { 
    Test.c <- gsub(paste0("\\\\b", i, "\\\\b"), "", Test) 
} 
Test.c <- gsub("[ ]+", " ", Test.c) 
Test.c <- gsub("^[[:space:]]|[[:space:]]$", "", Test.c) 
Test.c 
# "is an example text []" 
関連する問題