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)+!"]
]
精彩评论