Escaping Angled Bracket acts similar to look-ahead

拜拜、爱过 提交于 2019-12-01 21:08:14

\\> is a word boundary Which matches between a word character(in the left side) and a non-word character (in the right side) or end of the line anchor $.

> strings <- c("ten>eight", "ten_>_eight")
> gsub("\\>", "greater_", strings)
[1] "tengreater_>eightgreater_"   "ten_greater_>_eightgreater_"

In the above example it match only the word boundary exists between a word character after n and a non-word character > then also the boundary between t and end of the line anchor in the first element. And it matches between _ (also a word character) and > then between t and end of the line anchor (ie, $) in the second element. Finally it replaces the matched boundaries with the string you specified.

A simple example:

> gsub("\\>", "*", "f:r(:")
[1] "f*:r*(:"

Consider the below input string. (w means a word character, N means a non-word character)

    f:r(:
w___|||||
     |w|N
     N |
       |
       N

So \\> matches between,

  • f and :
  • r and (

Example 2:

> gsub("\\>", "*", "f") 
[1] "f*"

Input string:

f$
||----End of the line anchor
w

Replacing the matched boundary with * will give the above result.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!