Substituting lone characters or chars separated by comma

时间:2019-04-16 23:10:43

标签: r regex gsub

I have the following data and am looking to substitute only single characters.

A,Apple
A

I want to produce the output such that

Banana,Apple
Banana

In other words I want to substitute anything that has an A, or just an A with banana. But if another word starting with A comes in, I want to ignore that.

I tried

gsub("A", "Banana"),
gsub("A[^,;]","Banana"),

But this wont work for the first example, the output I get is

Banana,Bpple

Any ideas on how I can achieve this? Thanks!

2 个答案:

答案 0 :(得分:1)

If the value is always surrounded by punctuation or line start/end:

text = "A,Apple\nA\nAvocado"
text2 = gsub("(\\b)A(\\b)", "\\1Bananna\\2", text, TRUE, TRUE)
cat(text2)

This captures the punctuation, if any exist, around the "A", and then puts them back using the backreferences \1 and \2. PCRE are used so we can use the \b word boundary match.

Output:

Bananna,Apple
Bananna
Avocado

答案 1 :(得分:1)

A non-regex solution could be to split the string on comma (,), change the value to "Banana" if it is equal to "A"

sapply(strsplit(x, ","), function(x) toString(ifelse(x == "A","banana", x)))
#[1] "banana, Apple" "banana"

data

x <- c("A,Apple", "A")