-4

How to Replace the string inside the semicolon and the comma to "X" using R regex.

Input:
My name : Harry, Age : 23, Address : London,

Output:
My name : X, Age : X, Address : X,

Anshuman
  • 39
  • 1
  • 5
  • 2
    Please, show us what you have tried and add a label with relevant programming language or tool. – mrzasa Jun 25 '19 at 12:44

3 Answers3

2
gsub(": .*?,", ": X,", "My name : Harry, Age : 23, Address : London,")

#[1] "My name : X, Age : X, Address : X,"
Dan
  • 11,370
  • 4
  • 43
  • 68
  • while this answer works, and although it is not to the best question, I feel like the answer could still do with some explanation. In R's help guidance for regex the use of `?` is explained as "The preceding item is optional and will be matched at most once." I do not understand how this causes the `.*` part to limit itself to its shortest option. – Aaron Hayman Jun 25 '19 at 13:12
  • 1
    @AaronHayman Adding the question mark effectively causes `.*` to be reluctant as opposed to greedy. There's a nice discussion [here](https://stackoverflow.com/questions/5319840/greedy-vs-reluctant-vs-possessive-quantifiers). – Dan Jun 25 '19 at 13:22
0

you can use gsub

gsub( "[a-zA-Z0-9]+[,]",": X, ","My name : Harry, Age : 23, Address : Londo")

MatthewR
  • 2,660
  • 5
  • 26
  • 37
0

You can use the gsub, This performs greedy search substitution.

gsub(':[^,]+,',': X,',"My name : Harry, Age : 23, Address : London,")
## [1] "My name : X, Age : X, Address : X,"

The regex will look for sequences that match the discription in the first argument.

[^,]+ matches to a sequence that contains no commas, this ensures that only the next comma can be considered as part of the overall sequence.

Aaron Hayman
  • 525
  • 3
  • 11