Home > Software design >  Find pattern and wrap between parenthesis the next letter
Find pattern and wrap between parenthesis the next letter

Time:10-05

I have to find different patterns in a data frame column, once it is found, the next letter should be wrapped between parentheses:

Data:

a <- c('(acetyl)RKJOEQLKQ', 'LDFEION(acetyl)EFNEOW')

if the pattern is: '(acetyl)' this is the output that I'd like to achieve:

Expected output:

b <- c('(R)KJOEQLKQ', 'LDFEION(E)FNEOW')

I know how that I can find the pattern with gsub:

b <- gsub('(acetyl)', replacement = '', a)

However, I'm not sure how to approach the wrapping between the parenthesis of the next letter after the pattern is found.

Any help would be appreciated.

CodePudding user response:

You can use

a <- c('(acetyl)RKJOEQLKQ', 'LDFEION(acetyl)EFNEOW')
gsub('\\(acetyl\\)(.)', '(\\1)', a)
## => [1] "(R)KJOEQLKQ"     "LDFEION(E)FNEOW"

See the regex demo and the online R demo.

Details:

  • \(acetyl\) - matches a literal string (acetyl)
  • (.) - captures into Group 1 any single char

The (\1) replacement pattern replaces the matches with ( Group 1 value ).

  • Related