16

今天在talkstats.com 上遇到了一个问题,发帖人想使用正则表达式(不是strsplit)删除字符串的最后一个句点。我试图这样做,但没有成功。

N <- c("59.22.07", "58.01.32", "57.26.49")

#my attempts:
gsub("(!?\\.)", "", N)
gsub("([\\.]?!)", "", N)

我们如何删除字符串中的最后一个句点以获得:

[1] "59.2207" "58.0132" "57.2649"
4

4 回答 4

24

Maybe this reads a little better:

gsub("(.*)\\.(.*)", "\\1\\2", N)
[1] "59.2207" "58.0132" "57.2649"

Because it is greedy, the first (.*) will match everything up to the last . and store it in \\1. The second (.*) will match everything after the last . and store it in \\2.

It is a general answer in the sense you can replace the \\. with any character of your choice to remove the last occurence of that character. It is only one replacement to do!

You can even do:

gsub("(.*)\\.", "\\1", N)
于 2013-01-25T20:15:16.523 回答
14

你需要这个正则表达式: -

[.](?=[^.]*$)

并将其替换为空字符串。

所以,它应该是这样的: -

gsub("[.](?=[^.]*$)","",N,perl = TRUE)

解释: -

[.]         // Match a dot
(?=         // Followed by
    [^.]    // Any character that is not a dot.
     *      // with 0 or more repetition
     $      // Till the end. So, there should not be any dot after the dot we match.
)  

因此,只要在dot(.)前瞻中匹配 a ,匹配就会失败,因为dot在当前点之后的某个位置,模式正在匹配。

于 2013-01-25T20:03:33.473 回答
5

I'm sure you know this by now since you use stringi in your packages, but you can simply do

N <- c("59.22.07", "58.01.32", "57.26.49")

stringi::stri_replace_last_fixed(N, ".", "")
# [1] "59.2207" "58.0132" "57.2649"
于 2015-02-28T23:12:59.133 回答
2

我对我的正则表达式很懒惰,但这有效:

gsub("(*)(.)([0-9]+$)","\\1\\3",N)

我倾向于采取与标准相反的方法。而不是替换“。” 使用长度为零的字符串,我只解析两边的两个部分。

于 2013-01-25T20:14:51.327 回答