Afiq Johari
Afiq Johari

Reputation: 1462

gsub ignore case on matching but output should follow original case

I want to match log-in regardless of its case but the resulting matches for log-in (with ignore case) should follow its original case.

txt <- 'log-in LOG-in LOG-IN LOG IN log-on LOG-on LOG-ON LOG ON 23-2'
have <- gsub(pattern = 'log[ -]in',replacement ='login' ,x = txt,ignore.case = T)
have 
> "login login login login log-on LOG-on LOG-ON LOG ON 23-2"

want
> "login LOGin LOGIN LOGIN log-on LOG-on LOG-ON LOG ON 23-2"

Upvotes: 1

Views: 1291

Answers (3)

Quixotic22
Quixotic22

Reputation: 2924

Alternatively if you wanted to do this for both login and logout in one quick like you could just replace the '-' or ' ' when it is following a 'g'.

want <- gsub(x = txt, pattern = "(?<=g)[- ]", '', perl = TRUE,ignore.case = T)

if you did just want login add a look ahead too.

want <- gsub(x = txt, pattern = "(?<=g)[- ](?=i)", '', perl = TRUE,ignore.case = T)

Upvotes: 1

Chris Ruehlemann
Chris Ruehlemann

Reputation: 21400

Try this:

gsub(pattern = '(log)[ -](in)', replacement ='\\1\\2' , x = txt, ignore.case = T)
[1] "login LOGin LOGIN LOGIN log-on LOG-on LOG-ON LOG ON 23-2"

This works with backreference: \\1refers back to the first capture group (log) and 'remembers' it in its exact form (including case), (log), while \\2 refers back to the second capture group (in)

Upvotes: 2

Kota Mori
Kota Mori

Reputation: 6740

One way is to name log and in part as a group and use "\1" and "\2" in the substitution text.

txt <- 'log-in LOG-in LOG-IN LOG IN log-on LOG-on LOG-ON LOG ON 23-2'
gsub("(log)[ -](in)", "\\1\\2", txt, ignore.case=TRUE)

Upvotes: 1

Related Questions