开发者

regular expression with space

开发者 https://www.devze.com 2023-03-10 17:06 出处:网络
I am using regular expression in R with the following code: > temp <- c(\"Herniorrhaphy, left inguinal\", \"Herniorrhaphy, right inguinal\")

I am using regular expression in R with the following code:

> temp <- c("Herniorrhaphy, left inguinal", "Herniorrhaphy, right inguinal")
> grep("Herniorrhaphy, [left|right] inguinal",temp)
integer(0)
> grep("Herniorrhaphy, [left inguinal|right inguinal]",temp)
[1] 1 2

I wonder why the two regular expressio开发者_如何转开发n give difference result, thanks.


According to regexp explanation in the documentation (http://stat.ethz.ch/R-manual/R-devel/library/base/html/regex.html):

Note that alternation does not work inside character classes, where | has its literal meaning.

That explains why the first alternative doesn't return any results because '[' and ']' characters denote a character class. The correct sytax should be:

grep("Herniorrhaphy, (left|right) inguinal",temp)

On my R, the second alternative also returns empty set as well:

> temp <- c("Herniorrhaphy, left inguinal", "Herniorrhaphy, right inguinal")
> grep("Herniorrhaphy, [left inguinal|right inguinal] inguinal",temp)
integer(0)
> 

Are you sure you are copying directly from the workspace?


I think you want brackets ( ) not character class [ ], ie

"Herniorrhaphy, (left|right) inguinal"
"Herniorrhaphy, (left inguinal|right inguinal)"
0

精彩评论

暂无评论...
验证码 换一张
取 消