<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>sniff.org &#187; regular expression</title>
	<atom:link href="http://www.sniff.org/tag/regular-expression/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.sniff.org</link>
	<description>If you're nosy, then sniff.org is the place for you!</description>
	<lastBuildDate>Wed, 02 Sep 2009 18:23:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>XPath and Regex living together</title>
		<link>http://www.sniff.org/2009/03/27/xpath-and-regex-living-together/</link>
		<comments>http://www.sniff.org/2009/03/27/xpath-and-regex-living-together/#comments</comments>
		<pubDate>Sat, 28 Mar 2009 00:50:18 +0000</pubDate>
		<dc:creator>Lance</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[regex]]></category>
		<category><![CDATA[regular expression]]></category>
		<category><![CDATA[xpath]]></category>

		<guid isPermaLink="false">http://www.sniff.org/?p=59</guid>
		<description><![CDATA[Today at work, I needed to come up with a regular expression that, given an xpath expression for a node, would return the xpath for that node&#8217;s parent. Originally, I wasn&#8217;t using regular expressions at all, but was just getting the substring of the xpath up to the index of the last occurrence of /. [...]]]></description>
			<content:encoded><![CDATA[<p>Today at work, I needed to come up with a regular expression that, given an xpath expression for a node, would return the xpath for that node&#8217;s parent. Originally, I wasn&#8217;t using regular expressions at all, but was just getting the substring of the xpath up to the index of the last occurrence of <code>/</code>. This works fine, so long as the xpath expression doesn&#8217;t contain any conditionals that contain a <code>/</code>, but what if you want to get the parent xpath from something like this:</p>
<p><code>/a/b[@x='/1/2/3']/c[@y='/4/5/6']</code></p>
<p>If the substring approach is used, <code>/a/b[@x='/1/2/3']/c[@y='/4/5</code> is returned instead of <code>/a/b[@x='/1/2/3']</code>! Hence the need for a regular expression.</p>
<p>I did some hacking around (as it turns out I was approaching things from the wrong point of view at first), did a few fruitless google searches, and finally gave up searching on regexlib.com.</p>
<p>Finally, I figured it out all on my own after an &#8220;aha!&#8221; moment of clarity.</p>
<p><code>^.+(?=(?&lt;!\[[^\]]*)/)</code></p>
<p>Behold! What this is doing is a greedy match of any character up to but not including a <code>/</code> that follows a <code>[</code> without a <code>]</code> in between. I hope this will help somebody down the road who has a similar problem to solve.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.sniff.org/2009/03/27/xpath-and-regex-living-together/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
