开发者

R how to match regex "_a (b)"

I am crazy about this questi开发者_Python百科on. In R language regex, how to match a pattern "_a (b)"? a and b both stand for a word, there is a space in front of (.


What do you mean by word? Let's assume a and b are words that only contain characters a-z (for both upper and lower case), this is what you could do.

# define the regular expression pattern
pattern <- "[_a-zA-Z]+ \\([a-zA-Z]+\\)"

# define a string to match against the pattern
string <- "This is a _hello (world) test."

# apply the pattern to the string using the `gsub` function
gsub(pattern, "REPLACED", string)

Outputs:

[1] "This is a REPLACED test."
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜