本文介绍了在 R 中形成和使用正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 R 的新手.我正在学习形成正则表达式的概念.

Som I am new to R. I was learning this concept of forming regular expressions.

即类似这样的"(\\2.\\3)".这些是什么?我的意思是,这些数字和符号代表什么?谁能用非常外行的语言解释一下这是什么意思?或者类似这样的,(\2.\4)(\2.\4),是什么意思?感谢您的帮助!

i.e. something like this "(\\2.\\3)". What are these? I mean, what do these numbers and notation represents? Can anyone explain in a very layman language what does this mean? Or something like this, (\2.\4)(\2.\4), what does it mean? Thanks for any help!

推荐答案

它们被称为 backreferences 回忆捕获组匹配的内容.捕获组可以通过将要分组的字符放在一组括号内来创建.反向引用在 R 中被指定为一个反斜杠 (\),两个反斜杠 (\\);后跟一个数字表示要召回的组号.

They are called backreferences which recall what was matched by a capturing group. A capturing group can be created by placing the characters to be grouped inside a set of parenthesis ( ). A backreference is specified as a backslash (\) in R, two backslashes (\\); followed by a digit indicating the number of the group to be recalled.

以下是使用反向引用替换通过捕获组 #2#3 ...

Below is an example replacing using backreferences to recall what was matched by capturing group #2 and #3 ...

x <- 'foo bar baz quz'
sub('(\\S+) (\\S+) (\\S+) (\\S+)', '(\\2.\\3)', x)
# [1] "(bar.baz)"

注意:替换中的左括号和右括号以及点是文字字符.

Note: The opening and closing parenthesis in the replacement along with the dot are literal characters.

这篇关于在 R 中形成和使用正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 23:01