<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
     xmlns:admin="http://webns.net/mvcb/"
     xmlns:content="http://purl.org/rss/1.0/modules/content/"
     xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
	<channel> 

	<title>Comments on: Regex Alms for the Perl-less?</title>
	<link>http://ask.metafilter.com/79601/Regex-Alms-for-the-Perlless/</link>
	<description>Comments on Ask MetaFilter post Regex Alms for the Perl-less?</description>
	<pubDate>Fri, 28 Dec 2007 02:23:16 -0800</pubDate>
	<lastBuildDate>Fri, 28 Dec 2007 02:23:16 -0800</lastBuildDate>
	<language>en-us</language>
	<docs>http://blogs.law.harvard.edu/tech/rss</docs>
	<ttl>60</ttl>

	<item>
		<title>Question: Regex Alms for the Perl-less?</title>
		<link>http://ask.metafilter.com/79601/Regex-Alms-for-the-Perlless</link>	
		<description>Help me composite some regex.

&lt;small&gt;(That phrasing makes this question sound &lt;em&gt;way&lt;/em&gt; less nerdy than it is)&lt;/small&gt; &lt;br /&gt;&lt;br /&gt; Here&apos;s a regex to find some stuff between square brackets:&lt;br&gt;
/\[[^\]]+\]/&lt;br&gt;
&lt;br&gt;
Here&apos;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 &quot;Alt___ &quot; I&apos;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 &lt;em&gt;do&lt;/em&gt;.]:&lt;br&gt;
/(A|a)lt(\.|ernat(e|ive))?:?\s+/&lt;br&gt;
&lt;br&gt;
So what I need is a regular expression for &quot;stuff between square brackets where the first thing inside the brackets will NOT match the second regex.&quot; Or &quot;Stuff inside square brackets that begins with anything BUT alt or Alt. or Alternate or alternative: or alt.: or etc. etc.&lt;br&gt;
&lt;br&gt;
I feel like this should be easy, but I never bothered to totally and completely grok regex, and obviously I&apos;m hurting now because of it.  I&apos;d very much appreciate any help anyone could give, and in exchange you&apos;ll get co-author credit for the &lt;strong&gt;amazing&lt;/strong&gt; piece of software that this thing will ultimately be a part of!  ;-)</description>
		<guid isPermaLink="false">post:ask.metafilter.com,2007:site.79601</guid>
		<pubDate>Fri, 28 Dec 2007 02:12:28 -0800</pubDate>
		<dc:creator>ChasFile</dc:creator>
		
			<category>regex</category>
		
			<category>regularexpression</category>
		
			<category>regularexpressions</category>
		
			<category>perl</category>
		
			<category>headache</category>
		
	</item> <item>
		<title>By: spaceman_spiff</title>
		<link>http://ask.metafilter.com/79601/Regex-Alms-for-the-Perlless#1181424</link>	
		<description>You can (at least if it&apos;s like sed&apos;s regexes) rewrite the first one as:&lt;br&gt;
 /\[\([^\]]+\)\]/\1&lt;br&gt;
&lt;br&gt;
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.&lt;br&gt;
&lt;br&gt;
There&apos;s probably a way to chain them, too, but I think it&apos;s more readable to do one thing at a time.  Just my personal preference.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2007:site.79601-1181424</guid>
		<pubDate>Fri, 28 Dec 2007 02:23:16 -0800</pubDate>
		<dc:creator>spaceman_spiff</dc:creator>
	</item><item>
		<title>By: spaceman_spiff</title>
		<link>http://ask.metafilter.com/79601/Regex-Alms-for-the-Perlless#1181426</link>	
		<description>Never mind, I missed the &quot;not the second regex&quot; part.&lt;br&gt;
  Go ahead and revoke my geek card now.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2007:site.79601-1181426</guid>
		<pubDate>Fri, 28 Dec 2007 02:26:24 -0800</pubDate>
		<dc:creator>spaceman_spiff</dc:creator>
	</item><item>
		<title>By: Canard de Vasco</title>
		<link>http://ask.metafilter.com/79601/Regex-Alms-for-the-Perlless#1181427</link>	
		<description>It can be done with the (?!...) syntax - google &quot;negative lookahead assertion&quot;.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2007:site.79601-1181427</guid>
		<pubDate>Fri, 28 Dec 2007 02:36:59 -0800</pubDate>
		<dc:creator>Canard de Vasco</dc:creator>
	</item><item>
		<title>By: flabdablet</title>
		<link>http://ask.metafilter.com/79601/Regex-Alms-for-the-Perlless#1181438</link>	
		<description>Which regex dialect are you using?  sed?  egrep?  perl?  VBScript?  Something else?</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2007:site.79601-1181438</guid>
		<pubDate>Fri, 28 Dec 2007 03:23:36 -0800</pubDate>
		<dc:creator>flabdablet</dc:creator>
	</item><item>
		<title>By: beerbajay</title>
		<link>http://ask.metafilter.com/79601/Regex-Alms-for-the-Perlless#1181449</link>	
		<description>Looks like perl from the tags.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2007:site.79601-1181449</guid>
		<pubDate>Fri, 28 Dec 2007 03:44:57 -0800</pubDate>
		<dc:creator>beerbajay</dc:creator>
	</item><item>
		<title>By: ChasFile</title>
		<link>http://ask.metafilter.com/79601/Regex-Alms-for-the-Perlless#1181456</link>	
		<description>&lt;em&gt;Which regex dialect are you using?&lt;/em&gt;&lt;br&gt;
&lt;br&gt;
Assume perl, but if you can get it going in one of the others, that&apos;s fine, too. I can translate it easily enough. Mostly right now I&apos;m just looking for a way out of the woods; I can worry about trimming the hedges and edging the paths later.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2007:site.79601-1181456</guid>
		<pubDate>Fri, 28 Dec 2007 04:13:24 -0800</pubDate>
		<dc:creator>ChasFile</dc:creator>
	</item><item>
		<title>By: duckstab</title>
		<link>http://ask.metafilter.com/79601/Regex-Alms-for-the-Perlless#1181479</link>	
		<description>What Canard de Vasco said: &lt;pre&gt; /\[(?![Aa]lt(?:\.|ernat(?:e|ive))?:?\s+)[^\]]+\]/ &lt;/pre&gt;</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2007:site.79601-1181479</guid>
		<pubDate>Fri, 28 Dec 2007 05:25:08 -0800</pubDate>
		<dc:creator>duckstab</dc:creator>
	</item><item>
		<title>By: themel</title>
		<link>http://ask.metafilter.com/79601/Regex-Alms-for-the-Perlless#1181480</link>	
		<description>Seconding Canard de Vasco, the perlre manual has a pretty good explanation. Mind the distinction between &quot;negative lookahead&quot; and &quot;lookbehind&quot;, not much magic about the rest.&lt;br&gt;
&lt;br&gt;
&lt;code&gt;&lt;br&gt;
themel@sophokles:~$ perl -npe &apos;$_ = /\[(?!(A|a)lt(\.|ernat(e|ive))?:?\s+)[^\]]+\]/ ? &lt;br&gt;
&quot;MATCH\n&quot; : &quot;NO MATCH\n&quot;;&lt;br&gt;
&apos;&lt;br&gt;
foo&lt;br&gt;
NO MATCH&lt;br&gt;
[bar]&lt;br&gt;
MATCH&lt;br&gt;
[AltBar]&lt;br&gt;
MATCH&lt;br&gt;
[Alt: Foo]&lt;br&gt;
NO MATCH&lt;br&gt;
[Alternative: bar]&lt;br&gt;
&lt;/code&gt;</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2007:site.79601-1181480</guid>
		<pubDate>Fri, 28 Dec 2007 05:26:11 -0800</pubDate>
		<dc:creator>themel</dc:creator>
	</item><item>
		<title>By: cmiller</title>
		<link>http://ask.metafilter.com/79601/Regex-Alms-for-the-Perlless#1181502</link>	
		<description>Do you require that the second part be not exactly like the first but is allowed to be slightly alt&lt;strong&gt;ernative&lt;/strong&gt;?  Check out this.  I added carriage returns to break it up.&lt;br&gt;
&lt;br&gt;
/&lt;br&gt;
\[&lt;br&gt;
(&lt;br&gt;
[Aa]lt(?:\.|ernat(?:e|ive))?:&lt;br&gt;
)&lt;br&gt;
\]&lt;br&gt;
\s+&lt;br&gt;
\[(?!\1)\]&lt;br&gt;
/&lt;br&gt;
&lt;br&gt;
That finds a &lt;em&gt;[Alt:] spaces [somethingElseNotExactlyTheSame]&lt;/em&gt;&lt;br&gt;
&lt;br&gt;
If you don&apos;t care about the second part being exactly the same, but must not be ANY variant of the &quot;alt&quot; word, then duplicate that expression in place of &lt;em&gt;\1&lt;/em&gt;.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2007:site.79601-1181502</guid>
		<pubDate>Fri, 28 Dec 2007 06:00:55 -0800</pubDate>
		<dc:creator>cmiller</dc:creator>
	</item><item>
		<title>By: kxr</title>
		<link>http://ask.metafilter.com/79601/Regex-Alms-for-the-Perlless#1181507</link>	
		<description>Looks like people have already answered the post, but as a side-note I &lt;em&gt;have&lt;/em&gt; to recommend &lt;a href=&apos;http://www.weitz.de/regex-coach/&apos;&gt;The Regex Coach&lt;/a&gt;, which is essentially an interactive regex interpreter. Whenever I need to figure out a complicated regex for a piece of Perl I&apos;m working on, I break this out and use it to come up with the regex and a bunch of test cases. Very helpful.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2007:site.79601-1181507</guid>
		<pubDate>Fri, 28 Dec 2007 06:12:06 -0800</pubDate>
		<dc:creator>kxr</dc:creator>
	</item><item>
		<title>By: harmfulray</title>
		<link>http://ask.metafilter.com/79601/Regex-Alms-for-the-Perlless#1181606</link>	
		<description>There&apos;s also &lt;a href=&quot;http://www.newartisans.com/downloads_files/regex-tool.el&quot;&gt;regex-tool&lt;/a&gt;, which supports interactive regex building within Emacs.  It supports both Emacs-style and Perl-style regular expressions.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2007:site.79601-1181606</guid>
		<pubDate>Fri, 28 Dec 2007 08:27:37 -0800</pubDate>
		<dc:creator>harmfulray</dc:creator>
	</item><item>
		<title>By: KirkJobSluder</title>
		<link>http://ask.metafilter.com/79601/Regex-Alms-for-the-Perlless#1181646</link>	
		<description>There is also nothing wrong with a &quot;get then filter&quot; design pattern in this case. I find in most cases if I want to exclude X now, I end up excluding Y and Z down the road.  &lt;br&gt;
&lt;br&gt;
&lt;pre&gt;alts = re.compile(r&apos;(A|a)lt(\.|ernat(e|ive))?:?\s+&apos;)&lt;br&gt;
brackets = re.compile(r&apos;\[[^\]]+\]&apos;)&lt;br&gt;
list = [x for x in brackets.findall(r&apos;[hh][Alternative ]&apos;) if (not alts.search(x))]&lt;/pre&gt;</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2007:site.79601-1181646</guid>
		<pubDate>Fri, 28 Dec 2007 09:00:40 -0800</pubDate>
		<dc:creator>KirkJobSluder</dc:creator>
	</item><item>
		<title>By: tomwheeler</title>
		<link>http://ask.metafilter.com/79601/Regex-Alms-for-the-Perlless#1182949</link>	
		<description>I would caution you to not include a regex so complex you have to ask about it here in actual production code.  Anyone else who maintains the program will likely have to ask about it &amp;mdash; or worse yet, misunderstand or break it.   You might even forget how it works when you look at this code again in a few years.&lt;br&gt;
&lt;br&gt;
While several people have given regexes that will work, I will encourage you to think about writing the code for clarity to humans.  Unless you&apos;re in a tight loop for some high-performance code, an if/else statement and a couple of simpler regexes may be better in the long run.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2007:site.79601-1182949</guid>
		<pubDate>Sat, 29 Dec 2007 13:38:50 -0800</pubDate>
		<dc:creator>tomwheeler</dc:creator>
	</item>
	</channel>
</rss>
