Replace Two Dots in a String with Gsub

Replace two dots in a string with gsub

If you are going to use fixed = TRUE, use the (non-interpreted) character .:

> gsub("..", ".", test, fixed = TRUE)

Otherwise, within regular expressions (fixed = FALSE), . has a special meaning (any character) so you'll want to prefix it with a backslash to mean "the dot character":

> gsub("\\.\\.", ".", test)
> gsub("\\.{2}", ".", test)

Replace dots using `gsub`

My recommendation would be to escape the "." character:

        spy$Identifier <- gsub("\\.", "/", spy$Identifier)

In regular expression, a period is a special character that matches any character. "Escaping" it tells the search to look for an actual period. In R's gsub this is accomplished with two backslashes (i.e.: "\\"). In other languages, it's often just one backslash.

gsub() in R is not replacing '.' (dot)

You may need to escape the . which is a special character that means "any character" (from @Mr Flick's comment)

 gsub('\\.', '-', x)
#[1] "2014-06-09"

Or

gsub('[.]', '-', x)
#[1] "2014-06-09"

Or as @Moix mentioned in the comments, we can also use fixed=TRUE instead of escaping the characters.

 gsub(".", "-", x, fixed = TRUE)

Replace multiple strings in one gsub() or chartr() statement in R?

You can use gsubfn

library(gsubfn)
gsubfn(".", list("'" = "", " " = "_"), x)
# [1] "ab_c"

Similarly, we can also use mgsub which allows multiple replacement with multiple pattern to search

mgsub::mgsub(x, c("'", " "), c("", "_"))
#[1] "ab_c"

Replace the dot at the end of a string in R

Try :

x <- "DEL.Xp22.11..ZFX."
x <- gsub("..", ' (', x, fixed = T)
x <- gsub("\\.$", ')', x)

Here I use the regex anchor '$' to signify the end of the word. And '\' to escape the '.' that is a regex special character.

gsub to extract string before and after dots from a vector in R?

If you need to get these two values separately, you can use

x <- c("Prayer: Lord. Have mercy on.")
gsub("^[^:]*:\\s*([^.]+).*","\\1",x)
## => [1] "Lord"
gsub("^[^:]*:\\s*[^.]+\\.\\s*([^.]+).*","\\1",x)
## => [1] "Have mercy on"

See the R demo online, regex #1 and regex #2 demos. It does not matter if you use sub or gsub with these regexps, they will work the same, although sub is more logical as all you need is replace the whole string with the value of the first capturing group.

Details

  • ^ - start of string
  • [^:]* - zero or more chars other than :
  • : - a colon
  • \s* - zero or more whitespaces
  • [^.]+ - one or more chars other than a dot
  • \. - a dot
  • \s* - zero or more whitespaces
  • ([^.]+) - Capturing group 1: one or more chars other than dots
  • .* - the rest of the string.

Usng R - gsub using code in replacement - Replace comma with full stop after pattern

Your sub is mostly correct, but you'll need a capture group (the brackets and backreference \\1) for the replacement.

Because we are "capturing" the upper case letters, therefore \\1 here represents the original upper case letters in your original strings. The only replacement here is \\. to \\,. In other words, we are replacing upper case letters ^(([[:upper:]]+) AND full stop \\. with it's original content \\1 AND comma \\,.

For more details you can visit this page.

test_names <- c("ADAM, Smith J.", "JOHNSON. Richard", "BROWN, Wilhelm K.", "DAVIS, Daniel")

sub("^([[:upper:]]+)\\.","\\1\\,",test_names)
[1] "ADAM, Smith J." "JOHNSON, Richard" "BROWN, Wilhelm K."
[4] "DAVIS, Daniel"

How to replace all dots in a string using JavaScript

You need to escape the . because it has the meaning of "an arbitrary character" in a regular expression.

mystring = mystring.replace(/\./g,' ')


Related Topics



Leave a reply



Submit