2016-12-16 37 views
-1

我試圖使用正則表達式來提取此字符串R中的服務器名稱(server101):之間你如何提取R中兩個字符之間的值?

值@和下面的第一期間

t<-c("Current CPU load - jvm machine[example network-app_svc_group_mem4]@server101.example.com") 

I」(。)我試過這個:

gsub('.*\\@(\\d+),(\\d+).*', '\\1', t) 

這似乎沒有工作,任何想法?

+0

'GSUB( '@([^] +)|', '\\ 1',T)' – rawr

回答

2

與stringr:

library(stringr) 
str_match(t, ".*@([^\\.]*)\\..*")[2] 
#[1] "server101" 
+0

僅供參考:'str_match'並不需要一個完整的字符串匹配。此外,字符類中的點被視爲字面點,不需要轉義。由於'@'後面有一些值,我認爲'+'(出現1次或多次)比'*'(零次或多次出現)更具邏輯性。因此,我將'str_match'使用以下模式:'「@([^。] +)」'。 –

+0

感謝@WiktorStribiżew的信息,非常感謝。 –

1

您可以使用下面的基礎R代碼來提取所述第一@後比.[^.]+)其他1+字符:

> t <- "Current CPU load - jvm machine[example network-app_svc_group_mem4]@server101.example.com" 
> pattern="@([^.]+)" 
> m <- regmatches(t,regexec(pattern,t)) 
> result = unlist(m)[2] 
> result 
[1] "server101" 

隨着regexec,可以訪問子集(捕獲組內容)。

online R demo

另一種方法是使用regmatches/regexpr有PCRE與(?<[email protected])回顧後,僅該字符存在的檢查,但並沒有把字符到匹配的正則表達式:

> result2 <- regmatches(t, regexpr("(?<[email protected])[^.]+", t, perl=TRUE)) 
> result2 
[1] "server101" 

一個乾淨的stringr的方法將是使用相同的PCRE正則表達式與str_extract(使用類似的(因爲它也支持lookarounds),ICU,正則表達式):

> library(stringr) 
> t<-c("Current CPU load - jvm machine[example network-app_svc_group_mem4]@server101.example.com") 
> str_extract(t, "(?<[email protected])[^.]+") 
[1] "server101" 
相關問題