i am trying to write a regex that produces the content in a string that is NOT in parentheses or brackets. The parentheses is always a year, and the brackets could contain any normal characters, upper and lower case. i was going about it by finding the brackets and parentheses and then [^\regex] to escape it (is this right?)
here’s the string:
s = 'Some words (1999) [THINGS]
and the regex:
/[^(\(\d{4}\))|\[.*\]]/
but this includes the characters inside the brackets see (http://rubular.com/r/bbpcnnGgCI)
everything works up until adding the [^\regex]
for example, this works to get (1999):
>> puts s.match(/\(\d{4}\)/)
(1999)
and for whats in brackets:
>> puts s.match(/\[.*\]/)
[THINGS]
but put them together using | for “or”:
>> puts s.match(/\(\d{4}\)|\[.*\]/)
(1999)
…it just matches the parentheses and its contents.
what’s going on here?
what am i doing wrong here?
Try this
/\(.+/which will match everything from the opening(onwards. If you strip that out, you’re left with'Some words'which should be what you need?Two points
(appearing earlier in the string.By the way, I find this rather valuable when trying to come up with Regex patterns.
Edit This pattern should only match stuff in brackets even if there is a stray bracket earlier in the string.