开发者

Negative Lookbehind Regular Expression in Ruby - How do I match only "\(" and not "(" in Ruby?

I'm trying to write a regular expression that matches strings like these:

title(Hello+World)             #=> 开发者_如何学C[title, Hello+World]
title(\(Hello+World\))         #=> [title, \(Hello+World\)]
title((Hello+World))           #=> [title, (Hello+World)]
title(Well+Hello+World+!)      #=> [title, Well+Hello+World+!]
title(Well+\(Hello+World\)+!)  #=> [title, Well+\(Hello+World\)+!]
title(Well+(Hello+World)+!)    #=> [title, Well+(Hello+World)+!]

What's the simplest way to extract out a string from within a start ( and end ) token, given that token could be anywhere in the string it's extracting?


I am not sure those backslashes should remain the result or not, but here is the one without that, and yes, I am not using negative lookbehind here.

.scan(/(\w+)\(((?:\([^)]+\)|[^()]+)+)\)/)

=> [
["title", "Hello+World"], 
["title", "(Hello+World)"], 
["title", "(Hello+World)"], 
["title", "Well+Hello+World+!"], 
["title", "Well+(Hello+World)+!"], 
["title", "Well+(Hello+World)+!"]
]
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜