[英]Replace some text after a string with Regex and Gsub in R
It's a simple question, but I'm not good with Regex. 这是一个简单的问题,但我对Regex并不擅长。 (I tried many expressions without success)
(我尝试了许多表达但没有成功)
I want to replace all the text (replace for nothing) after a pattern. 我想在模式之后替换所有文本(替换为空)。
My pattern is something like this: 我的模式是这样的:
/canais/*/
My data is: 我的数据是:
/canais/b3/conheca-o-pai-dos-indices-da-b3/
/canais/cpbs/cvm-abre-audiencia-publica-de-instruc
/canais/stocche-forbes/dividendo-controverso/
The desired result is: 期望的结果是:
/canais/b3/
/canais/cpbs/
/canais/stocche-forbes/
How can I do it with gsub? 我怎么能用gsub呢?
Thanks 谢谢
You may use the following sub
: 您可以使用以下
sub
:
x <- c("/canais/b3/conheca-o-pai-dos-indices-da-b3/","/canais/cpbs/cvm-abre-audiencia-publica-de-instruc","/canais/stocche-forbes/dividendo-controverso/")
sub("^(/canais/[^/]+/).*", "\\1", x)
See the online R demo 查看在线R演示
Details : 细节 :
^
- start of string ^
- 字符串的开头 (/canais/[^/]+/)
- Group 1 (later referred to with \\1
) capturing: (/canais/[^/]+/)
- 组1(后面称为\\1
)捕获:
/canais/
- a substring /canais/
/canais/
- 子串/canais/
[^/]+
- 1 or more chars other than /
[^/]+
- 除/
之外的1个或更多字符 /
- a slash /
- 斜线 .*
- any 0+ chars up to the end of string. .*
- 字符串末尾的任何0+字符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.