Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove last occurrence of character

Tags:

regex

r

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 this but was unsuccessful.

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

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

How could we remove the last period in the string to get:

[1] "59.2207" "58.0132" "57.2649"
like image 734
Tyler Rinker Avatar asked Jan 25 '13 20:01

Tyler Rinker


People also ask

How do I remove the last occurrence of a character in Python?

Using replace() function. In Python, the string class provides a function replace(), and it helps to replace all the occurrences of a substring with another substring. We can use that to replace only the last occurrence of a substring in a string.

How do I remove the last character in Word?

substring() The easiest way is to use the built-in substring() method of the String class. In order to remove the last character of a given String, we have to use two parameters: 0 as the starting index, and the index of the penultimate character.

How do you find the last occurrence of a character?

strrchr() — Locate Last Occurrence of Character in String The strrchr() function finds the last occurrence of c (converted to a character) in string . The ending null character is considered part of the string . The strrchr() function returns a pointer to the last occurrence of c in string .

How do I remove the last 3 characters from a string?

To remove the last three characters from the string you can use string. Substring(Int32, Int32) and give it the starting index 0 and end index three less than the string length. It will get the substring before last three characters.


Video Answer


2 Answers

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)
like image 164
flodel Avatar answered Oct 10 '22 15:10

flodel


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.

like image 38
Rohit Jain Avatar answered Oct 10 '22 17:10

Rohit Jain