简体   繁体   中英

Extracting opening parenthesis in R

I'm trying to extract opening parenthesis in R. But getting the following error.

text = "suppressWarnings( Gauge_Rule_Extraction( machine_id, gauge_output_calculation_df, host ) )"

gregexpr( "(", text )

Error in gregexpr("(", m) : 
  invalid regular expression '(', reason 'Missing ')''

But I am able to extract closing parenthesis.

gregexpr(")", m)
[[1]]
[1] 123 125
attr(,"match.length")
[1] 1 1
attr(,"useBytes")
[1] TRUE

Anything I am doing wrong.

Brackets need to be escaped in regex. This is done with a back slash. Further, the backslase will then need to be escaped, so double up:

gregexpr( "\\(", text )

[[1]]
[1] 17 40
attr(,"match.length")
[1] 1 1
attr(,"useBytes")
[1] TRUE

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM