小编mae*_*del的帖子

如何在R中第一次出现逗号之前删除所有内容

我正在尝试删除文本,直到包含一个或多个逗号的字符串中的第一个逗号为止。出于某种原因,我发现这总是删除所有字符串的最后一个逗号之前的所有内容。

字符串看起来像:

OCR - (some text), Variant - (some text), Bad Subtype - (some text)
Run Code Online (Sandbox Code Playgroud)

我的正则表达式正在返回:

Bad Subtype - (some text)
Run Code Online (Sandbox Code Playgroud)

当所需的输出是:

Variant - (some text), Bad Subtype - (some text)
Run Code Online (Sandbox Code Playgroud)

Variant 不能保证排在第二位。

#select all strings beginning with OCR in the column Tags
clean<- subset(all, grepl("^OCR", all$Tags)
#trim the OCR text up to the first comma, and store in a new column called Tag
    clean$Tag<- gsub(".*,", "", clean$Tag) 
Run Code Online (Sandbox Code Playgroud)

或者

clean$Tag <- gsub(".*\\,", "", clean$Tag)
Run Code Online (Sandbox Code Playgroud)

或者

clean$Tag<- sub(".*,", "", clean$Tag)
Run Code Online (Sandbox Code Playgroud)

等等..

regex r

0
推荐指数
2
解决办法
107
查看次数

标签 统计

r ×1

regex ×1