How to replace some characters after the last dot with their lower cases

regex match everything until character
remove string after certain character javascript
regex match after first occurrence
regex replace
regex cheat sheet

A simple question but I cannot solve it. I got a string like this mail address:

ma <- "something@somewhere.COM"

My goal is to get:


So to put the to lower cases the part after the last dot. I've read this, this, so I tried:

gsub(".*\\.","\\L\\1", ma, perl = T) 
[1] "COM" # nope

Also something like:

options(gsubfn.engine = "R")
gsubfn(".*\\.", ~ tolower(x), ma)
[1] "something@somewhere.COM" # nope

I'm quite confused because it seems I can fetch the part I want to replace:

gsub(".*\\.","", ma)
[1] "COM"

But I cannot replace it properly. If you can give an explanation with the solution, I'll gladly appreciate it, regex is not my strongest feature.

\\L & \\U apply to a capturing group -- your first attempt is close but doesn't say which group to apply the command to:

ma <-"something@somewhere.COM"
gsub('(.*\\.)(.*)$', '\\1\\L\\2', ma, perl = TRUE)
# [1] ""

Note we capture two groups -- the part before (and including) the ., which we leave alone; and the part after the ., which we move to lower-case.

Also note it may be safer to use this regex to prevent any issues with greedy matching of .:

gsub('(.*\\.)([^.]*)$', '\\1\\L\\2', ma, perl = TRUE)
# [1] ""

Simple RegEx tricks for beginners, Always wanted to learn Regular Expressions but got put off by their complexity? The dot symbol . matches any character: The ? symbol after .* and some other RegEx sequences means “match as little as possible. You can use dash with letters too, [a-z] will match any lowercase Latin character, [A-Z]� If there are trailing spaces, they probably need to be removed in order to find the last (non-blank) character. So the solution would be. select RTRIM (RTRIM (Ename,' '),'.') from A. The inside RTRIM removes any trailing spaces. The outside RTRIM looks at the result and removes the trailing dot.

If you want to apply that to the second group of an email address, you could use an email like pattern with 2 capturing groups and use \\L for the second group.


For example

gsub("([^\\s@]+@[^\\s@]+\\.)([^\\s@]+)","\\1\\L\\2", "something@somewhere.COM", perl = T) 


[1] ""

R demo | Regex demo

Manipulating Characters in a String (The Java™ Tutorials , The index of the first character is 0, while the index of the last character is length ()-1 . Here are several other String methods for manipulating strings: String toUpperCase(), Returns a copy of this string converted to lowercase or uppercase. The String class does have four methods for replacing found characters or� If you want to replace a simple string and you don't need the abilities of regular expressions, you can just use replace, not replaceAll. replace replaces each matching substring but does not interpret its argument as a regular expression. str = xpath.replace(".", "/*/");

We can use sub to capture group and make use of \\L to change it to lowercase

sub("\\.(.*)$", ".\\L\\1", ma, perl = TRUE)
#[1] ""

How to remove portion of a string after certain character in JavaScript, How to remove portion of a string after certain character in JavaScript ? 29-05- It specifies the character, or the regular expression, to use for splitting the string. If not used How to get the last character of a string in JavaScript? How to replace lowercase letters with an other character in JavaScript ? Short and sweet, translate is superior to replace. If you're more interested in funcionality over time optimization, do not use replace. Also use translate if you don't know if the set of characters to be replaced overlaps the set of characters used to replace. Case in point:

The part after the @ in an email address is case insensitive so you could convert the entire part after @ to lower case without any problems.

We consider both cases.

Convert everything after last dot to lower case

To use gsubfn be sure that the regular expression is matching the extension.

Alternately we can make use of file_ext in the tools package (where the tools package comes with R so no installation is required).

A third approach is to use file_path_sans_ext together with file_ext (both from tools) and remove any trailing dot in case there was no extension. If we knew there is always an extension then the sub part can be omitted.

(Of course if we know that the part before the extension has no upper case characters or if we don't mind that it is converted to lower case we could just apply tolower to the entire input.)

s <- "something@somewhere.COM"

# 1
gsubfn("\\.[^.]*$", tolower, s)
## [1] ""

# 2
ext <- file_ext(s)
sub(paste0(ext, "$"), tolower(ext), s)
## [1] ""

# 3
sub("\\.$", "", paste(file_path_sans_ext(s), tolower(file_ext(s)), sep = "."))
## [1] ""
Convert everything after @ to lower case

As mentioned earlier the domain, i.e. the portion of the string after the @ character, is case insensitive so we can convert that entire part to lower case. This is a simpler problem as we are guaranteed that there is only one instance of @. We use gsubfn in (4), extract the parts before and after the @ in (5) and use read.table without any regular expressions in (6).

s <- "something@somewhere.COM"

# 4
gsubfn("@.*", tolower, s)
## [1] ""

# 5
paste(sub("@.*", "", s), tolower(sub(".*@", "", s)), sep = "@")
## [1] ""

# 6
with(read.table(text = s, sep = "@", = TRUE), paste(V1, tolower(V2), sep = "@"))
## [1] ""

How to remove portion of a string after a certain character in PHP , Function returns an integer value of position of first occurrence of string. Function treats upper-case and lower-case characters differently. Syntax: REPLACE Leave EMPTY ! Notes: The first part (?-s) is a modifier, which forces the regex engine to interpret the dot regex character as standing for any single standard character ( not End of Line characters ) The last part .* tries to match any range, even empty, of standard characters

Replace text using regular expression, If str is a single piece of text (either a character vector or a string scalar), then newStr is Replace lowercase letters at the beginning of sentences with their uppercase char. Any character with special meaning in regular expressions that you 'Joh?n\s(\w*)' captures a token that contains the last name of any person with� #14) Replace. Replace is used to replace the specified portion of a string with some other text as specified. The syntax of this is: Replace(name of the string, name of the string to be replaced, name of the new replaced string) #15) StrComp. StrComp is used to compare the 2 strings and return values on the basis of comparison.

String Manipulation and Regular Expressions, This section will cover some of Python's built-in string methods and formatting operations, To convert the entire string into upper-case or lower-case, you can use the The basic method of removing characters is the strip() method, which strips the first instance of the split-point, the split-point itself, and the substring after:. One can use replace() inside a loop to check for a bad_char and then replace it with the empty string hence removing it. This is the most basic approach and inefficient on performance point of view. This is the most basic approach and inefficient on performance point of view.

Ruby Strings Cheat Sheet, Return a lowercase copy of str # "STRING" => "string" Replace str from start to length with str2 Return a new string with last character removed from str # " string" => "strin" Convert pattern to regexp and invoke its match method on str Search str2 in str and return part before it, the match, and part after in an array. Java String replace method signature String replace (char oldChar, char newChar): It replaces all the occurrences of a oldChar character with newChar character. For e.g. "pog pance".replace ('p', 'd') would return dog dance.

  • Maybe try the tolower-function?
  • @Jaap tolower is going to work on the full string, I need to get the lower cases only for characters after the last dot.