0

In R, I have a character string w and would like to get rid of the SQL commented parts i.e. the characters between /* and */.

This is what I have tried so far, but it doesn't quite work....

 w <- "ldsjflsdj /* hhhhhhhhsdlfjlsj */ dskfhjsdkjfhsd"
 gsub("[/**/]","",w)

The ideal output would be something like:

`ldsjflsdj dskfhjsdkjfhsd`

How do I achieve this?

I am pretty sure it's simple I'm just not an expert in regex

Thanks,

HLM

4

2 回答 2

4

假设嵌套注释与在 C 中一样非法,则以下内容应该有效:

gsub("/\\*.*?\\*/", "", w);
于 2013-07-16T10:43:52.623 回答
2

使用qdap包的genX

library(qdap)
genX(w, "/\\*", "/\\*")

## [1] "ldsjflsdj dskfhjsdkjfhsd"
于 2013-07-16T11:12:27.760 回答