简体   繁体   English

在r中的两个“-”之间提取字符串

[英]Extracting string between two "-" in r

I am trying to understand how do i extract the string which is between two hyphens.我想了解如何提取两个连字符之间的字符串。

For example,例如,

node->testtransport-fasttrack-direct节点->testtransport-fasttrack-direct

I want the string fasttrack to be extracted and it shouldnt be based on the position of the strings as they might change.我希望提取字符串 fasttrack 并且它不应该基于字符串的位置,因为它们可能会改变。

I want the hard code to extract the string present between two hyphens我想要硬代码来提取两个连字符之间存在的字符串

Thank you in advance.先感谢您。

Here are some approaches.这里有一些方法。 No packages are used.不使用任何包。

1) Here we assume that the part between the two minus signs must be all upper case letters so >DHLPAKET is excluded because even though it is between two minus signs it has a character which is not an upper case letter. 1)这里我们假设两个减号之间的部分必须都是大写字母,所以>DHLPAKET被排除在外,因为即使它在两个减号之间,它也有一个不是大写字母的字符。 Match the start (^) and then anything (.*) followed by minus (-) followed by an upper case string which is captured ([AZ]+) and another minus (-) and everything else and finally the end of string ($).匹配开头 (^) 和任何 (.*) 后跟减号 (-) 后跟一个被捕获的大写字符串 ([AZ]+) 和另一个减号 (-) 以及其他所有内容,最后是字符串的结尾 ( $)。 Replace all that with the captured portion (\\1)用捕获的部分替换所有内容 (\\1)

x <- "WRO2->DHLPAKET-ASCHHEIM-DI"
sub("^.*-([A-Z]+)-.*$", "\\1", x)
## [1] "ASCHHEIM"

2) If the two minus signs surrounding the string of interest are always the second and third minus signs then this would work. 2)如果感兴趣的字符串周围的两个减号始终是第二个和第三个减号,那么这将起作用。 It uses read.table picking off the third minus-separated field.它使用read.table挑选出第三个减号分隔的字段。

read.table(text = x, sep = "-", as.is = TRUE)$V3
## [1] "ASCHHEIM"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM