Regex Alms for the Perl-less?
December 28, 2007 2:12 AM
Subscribe
Help me composite some regex.
(That phrasing makes this question sound way less nerdy than it is)
Here's a regex to find some stuff between square brackets:
/\[[^\]]+\]/
Here's one that finds something like Alt: or alt. or Alternative: or alternate or Alternate: or Alternative or Alt.: or you get the idea [note it ends with \s+; it is important for my application that the "Alt___ " I'm testing for has white space at the end, and that I test for it. In the final answer we can test for that word boundary any way we like, we just need to make sure that we do.]:
/(A|a)lt(\.|ernat(e|ive))?:?\s+/
So what I need is a regular expression for "stuff between square brackets where the first thing inside the brackets will NOT match the second regex." Or "Stuff inside square brackets that begins with anything BUT alt or Alt. or Alternate or alternative: or alt.: or etc. etc.
I feel like this should be easy, but I never bothered to totally and completely grok regex, and obviously I'm hurting now because of it. I'd very much appreciate any help anyone could give, and in exchange you'll get co-author credit for the amazing piece of software that this thing will ultimately be a part of! ;-)
posted by ChasFile to computers & internet (13 comments total)
4 users marked this as a favorite
/\[\([^\]]+\)\]/\1
And this will return anything within the parens (you can also use multiple sets of escaped parens, and reference them as \1, \2, \3). Then just pass it on to the next bit.
There's probably a way to chain them, too, but I think it's more readable to do one thing at a time. Just my personal preference.
posted by spaceman_spiff at 2:23 AM on December 28, 2007