[英]R: Extract substring and paste the same substring at the end of a string
I have some strings with the following pattern of: letters and numbers我有一些具有以下模式的字符串:字母和数字
A11B3XyC4
A1B14C23XyC16
B14C23XyC16D3
I want to extract the part "Xy" (always the same letters) and paste it at the end of the remaining string.我想提取“Xy”部分(总是相同的字母)并将其粘贴到剩余字符串的末尾。 The result schould look like this:
结果应该是这样的:
A11B3C4. Xy
A1B14C23C16. Xy
B14C23C16D3. Xy
Could you point me to a function capable of this?你能给我指一个有能力的 function 吗?
Thank you!谢谢你!
We can use sub
and build groups in the pattern
argument by wrapping them in ()
.我们可以在
pattern
参数中使用sub
和 build 组,方法是将它们包装在()
中。 We can access these groups in the replacement
argument with \\
followed by the group number.我们可以在
replacement
参数中使用\\
后跟组号来访问这些组。
strs <- c("A11B3XyC4",
"A1B14C23XyC16",
"B14C23XyC16D3")
sub("(.*)(Xy)(.*)", "\\1\\3\\. \\2", strs)
#> [1] "A11B3C4. Xy" "A1B14C23C16. Xy" "B14C23C16D3. Xy"
Created on 2021-08-27 by the reprex package (v0.3.0)由reprex package (v0.3.0) 创建于 2021-08-27
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.