regular expression with space
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)"
精彩评论