3

For instance I have

a=c("Jack and Jill,went up the, hill,to,fetch a pail,of, water")

and I'm trying to do is add space after comma if and only if the comma is followed by an alphabet such that my Output would look like this

 "Jack and Jill, went up the, hill, to, fetch a pail, of, water"

This is what I have tried

gsub("/,(?![ ])/, ", " ",a)

but doesn't give me desired result. Any help would be much appreciated. THANKS

Andre_k
  • 1,680
  • 3
  • 18
  • 41
  • 5
    `gsub(",(?!\\s)",", ",a,perl=TRUE)` Or also `gsub(",(?=[A-Za-z])",", ",a,perl=TRUE)`. – nicola May 24 '17 at 10:24
  • This command even worked even if the case of numbers for eg. ..... a=c("my address,is, plot no 3,2nd street, lake cross, nz")....Much thanks – Andre_k May 24 '17 at 10:33

1 Answers1

6

We can use gsub to match a comma (,) followed by any letter (([A-Za-z])) captured as a group and then replace it with , followed by a space and the backreference of that captured group (\\1)

gsub(",([A-Za-z])", ", \\1", a)
#[1] "Jack and Jill, went up the, hill, to, fetch a pail, of, water"

Or use [[:alpha:]]

gsub(",([[:alpha:]])", ", \\1", a)
#[1] "Jack and Jill, went up the, hill, to, fetch a pail, of, water"
akrun
  • 874,273
  • 37
  • 540
  • 662
  • 2
    The `[A-z]` is a misleading character class. People believe it matches letters only, but [it is not so](http://stackoverflow.com/a/29771926/3832970). – Wiktor Stribiżew May 24 '17 at 10:26
  • @WiktorStribiżew Thanks, I was not aware about that. One time when I used `[A-Za-z]` somebody commented that it is much easier to do `[A-z]`. Should have checked it. Thanks for enligtening me – akrun May 24 '17 at 10:29
  • 1
    The problem is that this wrong pattern is used even [in some Web help](http://stat545.com/block022_regular-expression.html). – Wiktor Stribiżew May 24 '17 at 10:33