在字符串的句点之前删除所有字符

时间:2021-12-05 23:32:18

This keeps everything before a period:

这让一切都提前了一个周期:

gsub("\\..*","", data$column )

how to keep everything after the period?

经期后如何保存一切?

4 个解决方案

#1


14  

To remove all the characters before a period in a string(including period).

删除字符串中句点(包括句点)前的所有字符。

gsub("^.*\\.","", data$column )

Example:

例子:

> data <- 'foobar.barfoo'
> gsub("^.*\\.","", data)
[1] "barfoo"

To remove all the characters before the first period(including period).

在第一个周期(包括周期)之前删除所有字符。

> data <- 'foo.bar.barfoo'
> gsub("^.*?\\.","", data)
[1] "bar.barfoo"

#2


2  

You could use stringi with lookbehind regex

你可以使用stringi和lookbehind regex。

 library(stringi)
 stri_extract_first_regex(data1, "(?<=\\.).*")
 #[1] "bar.barfoo"
 stri_extract_first_regex(data, "(?<=\\.).*")
 #[1] "barfoo"

If the string doesn't have ., this retuns NA (it is not clear about how to deal with this in the question)

如果字符串没有,这个retuns NA(问题中不清楚如何处理)

 stri_extract_first_regex(data2, "(?<=\\.).*")
 #[1] NA

###data
data <- 'foobar.barfoo' 
data1 <- 'foo.bar.barfoo'
data2 <- "foobar"

#3


1  

use this :

用这个:

gsub(".*\\.","", data$column )

this will keep everything after period

这将使以后的一切

#4


1  

If you don't want to think about the regex for this the qdap package has the char2end function that grabs from a particular character until the end of the string.

如果您不想考虑这个正则表达式,那么qdap包有一个char2end函数,它可以从一个特定的字符捕获到字符串的末尾。

data <- c("foo.bar", "foo.bar.barfoo")

library(qdap)
char2end(data, ".")

## [1] "bar"        "bar.barfoo"

#1


14  

To remove all the characters before a period in a string(including period).

删除字符串中句点(包括句点)前的所有字符。

gsub("^.*\\.","", data$column )

Example:

例子:

> data <- 'foobar.barfoo'
> gsub("^.*\\.","", data)
[1] "barfoo"

To remove all the characters before the first period(including period).

在第一个周期(包括周期)之前删除所有字符。

> data <- 'foo.bar.barfoo'
> gsub("^.*?\\.","", data)
[1] "bar.barfoo"

#2


2  

You could use stringi with lookbehind regex

你可以使用stringi和lookbehind regex。

 library(stringi)
 stri_extract_first_regex(data1, "(?<=\\.).*")
 #[1] "bar.barfoo"
 stri_extract_first_regex(data, "(?<=\\.).*")
 #[1] "barfoo"

If the string doesn't have ., this retuns NA (it is not clear about how to deal with this in the question)

如果字符串没有,这个retuns NA(问题中不清楚如何处理)

 stri_extract_first_regex(data2, "(?<=\\.).*")
 #[1] NA

###data
data <- 'foobar.barfoo' 
data1 <- 'foo.bar.barfoo'
data2 <- "foobar"

#3


1  

use this :

用这个:

gsub(".*\\.","", data$column )

this will keep everything after period

这将使以后的一切

#4


1  

If you don't want to think about the regex for this the qdap package has the char2end function that grabs from a particular character until the end of the string.

如果您不想考虑这个正则表达式,那么qdap包有一个char2end函数,它可以从一个特定的字符捕获到字符串的末尾。

data <- c("foo.bar", "foo.bar.barfoo")

library(qdap)
char2end(data, ".")

## [1] "bar"        "bar.barfoo"