如何替换“ |〜|| ~~ ||〜||〜|” R中有空格?

时间:2018-06-26 14:15:05

标签: r

我有某种数据准备,其中某些字符串“ |〜||〜||〜|||〜|”的结尾或“ |〜||〜|”人物来了。我无法通过gsub()和str_replace()函数将其删除。请指教。

2 个答案:

答案 0 :(得分:3)

只需要转义|,即\\|

string <- c("cat|~||~||~||~|", "mouse|~||~|", "dogg")
sub("\\|~.+$", "", string)
[1] "cat"   "mouse" "dogg" 

或者,您可以指定fixed = TRUE

gsub("|~|", "", string, fixed = TRUE)
[1] "cat"   "mouse" "dogg" 

答案 1 :(得分:2)

问题在于字符“ |”是正则表达式解释为并/或的特殊字符或元字符。您可以使用“ \”将其换行,也可以强制正则表达式使用“ [|]”来表示文字字符。

尝试一下:

class HcpFormTest : ESIntegTestCase() {

    override fun nodeSettings(nodeOrdinal: Int): Settings = Settings.builder()
        .put(super.nodeSettings(nodeOrdinal))
        //.put("node.mode", "network")
        .build()

    @org.junit.Test fun start() {
        ensureGreen()
    }
}

结果:

java.lang.RuntimeException: found jar hell in test classpath

    at org.elasticsearch.bootstrap.BootstrapForTesting.<clinit>(BootstrapForTesting.java:92)
    at org.elasticsearch.test.ESTestCase.<clinit>(ESTestCase.java:190)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:348)
    at com.carrotsearch.randomizedtesting.RandomizedRunner$2.run(RandomizedRunner.java:592)
Caused by: java.lang.IllegalStateException: jar hell!
duplicate jar [C:\Users\avo_apurebase\AppData\Local\JetBrains\Toolbox\apps\IDEA-U\ch-0\181.5281.24\lib\idea_rt.jar] on classpath: ...
    at org.elasticsearch.bootstrap.JarHell.parseClassPath(JarHell.java:142)
    at org.elasticsearch.bootstrap.JarHell.parseClassPath(JarHell.java:98)
    at org.elasticsearch.bootstrap.JarHell.checkJarHell(JarHell.java:89)
    at org.elasticsearch.bootstrap.BootstrapForTesting.<clinit>(BootstrapForTesting.java:90)
    ... 4 more