Remove last occurrence of character

前端 未结 4 1342
醉梦人生
醉梦人生 2021-02-07 11:58

A question came across talkstats.com today in which the poster wanted to remove the last period of a string using regex (not strsplit). I made an attempt to do thi

相关标签:
4条回答
  • 2021-02-07 12:07

    I'm pretty lazy with my regex, but this works:

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

    I tend to take the opposite approach from the standard. Instead of replacing the '.' with a zero-length string, I just parse the two pieces that are on either side.

    0 讨论(0)
  • 2021-02-07 12:11

    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)
    
    0 讨论(0)
  • 2021-02-07 12:13

    You need this regex: -

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

    And replace it with empty string.

    So, it should be like: -

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

    Explanation: -

    [.]         // 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.
    )  
    

    So, as soon as a dot(.) is matched in the look-ahead, the match is failed, because, there is a dot somewhere after the current dot, the pattern is matching.

    0 讨论(0)
  • 2021-02-07 12:19

    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"
    
    0 讨论(0)
提交回复
热议问题