[英]How can I remove all characters after a specified word and before a period in a string using javascript?
[英]Remove all characters before a period in a string
這樣可以在一段時間之前保留所
gsub("\\..*","", data$column )
如何保持一段時間后的一切?
刪除字符串中句點之前的所有字符( 包括句點 )。
gsub("^.*\\.","", data$column )
例:
> data <- 'foobar.barfoo'
> gsub("^.*\\.","", data)
[1] "barfoo"
刪除第一個句點( 包括句號 )之前的所有字符。
> data <- 'foo.bar.barfoo'
> gsub("^.*?\\.","", data)
[1] "bar.barfoo"
你可以使用stringi
與lookbehind
正則表達式
library(stringi)
stri_extract_first_regex(data1, "(?<=\\.).*")
#[1] "bar.barfoo"
stri_extract_first_regex(data, "(?<=\\.).*")
#[1] "barfoo"
如果字符串沒有.
,這回歸NA
(在問題中如何處理這個問題尚不清楚)
stri_extract_first_regex(data2, "(?<=\\.).*")
#[1] NA
###data
data <- 'foobar.barfoo'
data1 <- 'foo.bar.barfoo'
data2 <- "foobar"
用這個 :
gsub(".*\\.","", data$column )
這將保持一段時間后的一切
如果您不想考慮此正則表達式,則qdap
包具有char2end
函數,該函數從特定字符抓取直到字符串結尾。
data <- c("foo.bar", "foo.bar.barfoo")
library(qdap)
char2end(data, ".")
## [1] "bar" "bar.barfoo"
require(stringr)
我開設了數據分析課程,學生們提出了這個解決方案:
get_after_period <- function(my_vector) {
# Return a string vector without the characters
# before a period (excluding the period)
# my_vector, a string vector
str_sub(my_vector, str_locate(my_vector, "\\.")[,1]+1)
}
現在,只需調用函數:
my_vector <- c('foobar.barfoo', 'amazing.point')
get_after_period(my_vector)
[1] "barfoo" "point"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.