<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Regex Walk-Through: Match filename base and extension</title>
	<atom:link href="http://www.movingtofreedom.org/2008/04/01/regex-match-filename-base-and-extension/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.movingtofreedom.org/2008/04/01/regex-match-filename-base-and-extension/</link>
	<description>free software, free culture, free association</description>
	<lastBuildDate>Tue, 16 Mar 2010 01:39:52 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: shawno</title>
		<link>http://www.movingtofreedom.org/2008/04/01/regex-match-filename-base-and-extension/comment-page-1/#comment-3925</link>
		<dc:creator>shawno</dc:creator>
		<pubDate>Thu, 31 Dec 2009 19:50:52 +0000</pubDate>
		<guid isPermaLink="false">http://www.movingtofreedom.org/2008/04/01/regex-match-filename-base-and-extension/#comment-3925</guid>
		<description>Excellent.  Thanks Scott.  Looks like (for option 2) &quot;match optional beginning period followed by chars not period nor newline followed by optional period&quot;.  Qt likes it.  Awesome!  I&#039;ve been away too long...  Shawn</description>
		<content:encoded><![CDATA[<p>Excellent.  Thanks Scott.  Looks like (for option 2) "match optional beginning period followed by chars not period nor newline followed by optional period".  Qt likes it.  Awesome!  I've been away too long...  Shawn</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Scott Carpenter</title>
		<link>http://www.movingtofreedom.org/2008/04/01/regex-match-filename-base-and-extension/comment-page-1/#comment-3924</link>
		<dc:creator>Scott Carpenter</dc:creator>
		<pubDate>Wed, 30 Dec 2009 14:07:26 +0000</pubDate>
		<guid isPermaLink="false">http://www.movingtofreedom.org/2008/04/01/regex-match-filename-base-and-extension/#comment-3924</guid>
		<description>&lt;p&gt;Hi, shawno -- using my &lt;a href=&quot;http://www.movingtofreedom.org/2008/03/29/python-regex-test-function/&quot; rel=&quot;nofollow&quot;&gt;Python regex test function&lt;/a&gt; for a list like:&lt;/p&gt;

&lt;pre&gt;file1.txt
file2.txt
file3
file4.
.file5
file6.txt
file7
file8.txt&lt;/pre&gt;

&lt;p&gt;I came up with &lt;code class=&quot;re&quot;&gt;(?m)^[^.\n]+\.?$&lt;/code&gt;. If you want to include &lt;code&gt;.file5&lt;/code&gt; with the &quot;no extension&quot; files, then &lt;code class=&quot;re&quot;&gt;(?m)^\.?[^.\n]+\.?$&lt;/code&gt;. (&lt;code class=&quot;re&quot;&gt;(?m)&lt;/code&gt; is the multiline flag which causes ^$ to match beginning/end of lines in addition to the normal beginning/end of the whole string.)&lt;/p&gt;

Interactively:

&lt;pre class=&quot;code&quot;&gt;from imisc import match
list = &#039;file1.txt\nfile2.txt\nfile3\nfile4.\n.file5\nfile6.txt\nfile7\nfile8.txt&#039;
match(&#039;(?m)^\.?[^.\n]+\.?$&#039;, list)&lt;/pre&gt;

With results:

&lt;pre class=&quot;op&quot;&gt;a match!
1) start: 20, end: 25, str: file3
2) start: 26, end: 32, str: file4.
3) start: 33, end: 39, str: .file5
4) start: 50, end: 55, str: file7
global replace (_._):
file1.txt
file2.txt
_._
_._
_._
file6.txt
_._
file8.txt&lt;/pre&gt;

&lt;p&gt;It&#039;s not Qt, but it&#039;s what I know. :-)&lt;/p&gt;</description>
		<content:encoded><![CDATA[<p>Hi, shawno -- using my <a href="http://www.movingtofreedom.org/2008/03/29/python-regex-test-function/" rel="nofollow">Python regex test function</a> for a list like:</p>
<pre>file1.txt
file2.txt
file3
file4.
.file5
file6.txt
file7
file8.txt</pre>
<p>I came up with <code class="re">(?m)^[^.\n]+\.?$</code>. If you want to include <code>.file5</code> with the "no extension" files, then <code class="re">(?m)^\.?[^.\n]+\.?$</code>. (<code class="re">(?m)</code> is the multiline flag which causes ^$ to match beginning/end of lines in addition to the normal beginning/end of the whole string.)</p>
<p>Interactively:</p>
<pre class="code">from imisc import match
list = 'file1.txt\nfile2.txt\nfile3\nfile4.\n.file5\nfile6.txt\nfile7\nfile8.txt'
match('(?m)^\.?[^.\n]+\.?$', list)</pre>
<p>With results:</p>
<pre class="op">a match!
1) start: 20, end: 25, str: file3
2) start: 26, end: 32, str: file4.
3) start: 33, end: 39, str: .file5
4) start: 50, end: 55, str: file7
global replace (_._):
file1.txt
file2.txt
_._
_._
_._
file6.txt
_._
file8.txt</pre>
<p>It's not Qt, but it's what I know. :-)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: shawno</title>
		<link>http://www.movingtofreedom.org/2008/04/01/regex-match-filename-base-and-extension/comment-page-1/#comment-3923</link>
		<dc:creator>shawno</dc:creator>
		<pubDate>Wed, 30 Dec 2009 04:34:15 +0000</pubDate>
		<guid isPermaLink="false">http://www.movingtofreedom.org/2008/04/01/regex-match-filename-base-and-extension/#comment-3923</guid>
		<description>Your discussion probably goes beyond what I&#039;m looking for and I can&#039;t quite distill it down to what I need.  I&#039;ll probably buy the MRE OReilly book on eBay.  In the meantime...I&#039;m working on a search program built with Qt.  I need to filter a string list of filenames with a regular expression that will match (not capture) ONLY filenames with no extension.  So, &#039;hack.foo&#039;, &#039;hack.h&#039;, and &#039;h.hack&#039; would be excluded; whereas &#039;hack&#039; and &#039;hack.&#039; would be included.  I guess I&#039;d be open to how to handle &#039;.hack&#039;.  If you&#039;d be so kind I&#039;d sure appreciate a suggestion.
Thanks, shawno</description>
		<content:encoded><![CDATA[<p>Your discussion probably goes beyond what I'm looking for and I can't quite distill it down to what I need.  I'll probably buy the MRE OReilly book on eBay.  In the meantime...I'm working on a search program built with Qt.  I need to filter a string list of filenames with a regular expression that will match (not capture) ONLY filenames with no extension.  So, 'hack.foo', 'hack.h', and 'h.hack' would be excluded; whereas 'hack' and 'hack.' would be included.  I guess I'd be open to how to handle '.hack'.  If you'd be so kind I'd sure appreciate a suggestion.<br />
Thanks, shawno</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Scott Carpenter</title>
		<link>http://www.movingtofreedom.org/2008/04/01/regex-match-filename-base-and-extension/comment-page-1/#comment-3780</link>
		<dc:creator>Scott Carpenter</dc:creator>
		<pubDate>Mon, 06 Apr 2009 21:31:09 +0000</pubDate>
		<guid isPermaLink="false">http://www.movingtofreedom.org/2008/04/01/regex-match-filename-base-and-extension/#comment-3780</guid>
		<description>Hi, Daniel.  Thanks for taking the time to comment and provide feedback.  It&#039;s good to know that these things are useful from time to time.

I experimented with your variation and it works well for getting the &lt;code&gt;None&lt;/code&gt; result. In my case, I was reassembling base + &#039;new string&#039; + ext, so it was more appropriate to have the ext be an empty string instead of &lt;code&gt;None&lt;/code&gt;, but of course other cases would be nicer with &lt;code&gt;None&lt;/code&gt;.

And! In the end I went with the simpler Python method anyway:

&lt;pre class=&quot;code&quot;&gt;base, ext = os.path.splitext(filename)
new_filename = base + &#039;.descripted&#039; + ext&lt;/pre&gt;

&lt;p&gt;(Where they also decided to go with the empty string result when there is no extension.)&lt;/p&gt;

&lt;p&gt;:-)&lt;/p&gt;

&lt;p&gt;Thanks again for visiting!&lt;/p&gt;</description>
		<content:encoded><![CDATA[<p>Hi, Daniel.  Thanks for taking the time to comment and provide feedback.  It's good to know that these things are useful from time to time.</p>
<p>I experimented with your variation and it works well for getting the <code>None</code> result. In my case, I was reassembling base + 'new string' + ext, so it was more appropriate to have the ext be an empty string instead of <code>None</code>, but of course other cases would be nicer with <code>None</code>.</p>
<p>And! In the end I went with the simpler Python method anyway:</p>
<pre class="code">base, ext = os.path.splitext(filename)
new_filename = base + '.descripted' + ext</pre>
<p>(Where they also decided to go with the empty string result when there is no extension.)</p>
<p>:-)</p>
<p>Thanks again for visiting!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Daniel</title>
		<link>http://www.movingtofreedom.org/2008/04/01/regex-match-filename-base-and-extension/comment-page-1/#comment-3779</link>
		<dc:creator>Daniel</dc:creator>
		<pubDate>Mon, 06 Apr 2009 19:18:57 +0000</pubDate>
		<guid isPermaLink="false">http://www.movingtofreedom.org/2008/04/01/regex-match-filename-base-and-extension/#comment-3779</guid>
		<description>Hi Scott,

I really appreciated your article. It saved me a lot of time not having to find it out by myself. You do point out a very elegant but simple solution!

Matching filenames with regex is useful to validate filenames that are expected to satisfy rules expressed as patterns stored somewhere else.

However, I would suggest a minor change in your expression:
&lt;code class=&quot;re&quot;&gt;(.+?)(?:\.([^.]*$)&#124;$)&lt;/code&gt; or &lt;code class=&quot;re&quot;&gt;(.+?)(?:(\.[^.]*$)&#124;$)&lt;/code&gt;. It creates a group that contains the extension or &lt;code&gt;None&lt;/code&gt; if there is no extension (respectively, without or with the point of the extension). Personally, I prefer getting &lt;code&gt;None&lt;/code&gt; if an optional element is not present in the matched string.

Also, it might be useful matching the directory assuming a path to a file instead of a filename. By following your advice, I was easy to find out another regex pattern: &lt;code class=&quot;re&quot;&gt;^(.*/)?(?:$&#124;(.+?)(?:(\.[^.]*$)&#124;$))&lt;/code&gt;

Best regards, Daniel</description>
		<content:encoded><![CDATA[<p>Hi Scott,</p>
<p>I really appreciated your article. It saved me a lot of time not having to find it out by myself. You do point out a very elegant but simple solution!</p>
<p>Matching filenames with regex is useful to validate filenames that are expected to satisfy rules expressed as patterns stored somewhere else.</p>
<p>However, I would suggest a minor change in your expression:<br />
<code class="re">(.+?)(?:\.([^.]*$)|$)</code> or <code class="re">(.+?)(?:(\.[^.]*$)|$)</code>. It creates a group that contains the extension or <code>None</code> if there is no extension (respectively, without or with the point of the extension). Personally, I prefer getting <code>None</code> if an optional element is not present in the matched string.</p>
<p>Also, it might be useful matching the directory assuming a path to a file instead of a filename. By following your advice, I was easy to find out another regex pattern: <code class="re">^(.*/)?(?:$|(.+?)(?:(\.[^.]*$)|$))</code></p>
<p>Best regards, Daniel</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Scott Carpenter</title>
		<link>http://www.movingtofreedom.org/2008/04/01/regex-match-filename-base-and-extension/comment-page-1/#comment-3542</link>
		<dc:creator>Scott Carpenter</dc:creator>
		<pubDate>Tue, 15 Apr 2008 00:43:17 +0000</pubDate>
		<guid isPermaLink="false">http://www.movingtofreedom.org/2008/04/01/regex-match-filename-base-and-extension/#comment-3542</guid>
		<description>Hi, Bruce.  Thanks -- that looks good.  I wrote a GUI file renamer for Windows in Visual Basic once upon a time that I&#039;m still using via Wine, but have been wanting to replace it.  This one might do the trick!</description>
		<content:encoded><![CDATA[<p>Hi, Bruce.  Thanks -- that looks good.  I wrote a GUI file renamer for Windows in Visual Basic once upon a time that I'm still using via Wine, but have been wanting to replace it.  This one might do the trick!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bruce</title>
		<link>http://www.movingtofreedom.org/2008/04/01/regex-match-filename-base-and-extension/comment-page-1/#comment-3539</link>
		<dc:creator>Bruce</dc:creator>
		<pubDate>Mon, 14 Apr 2008 14:42:25 +0000</pubDate>
		<guid isPermaLink="false">http://www.movingtofreedom.org/2008/04/01/regex-match-filename-base-and-extension/#comment-3539</guid>
		<description>Mastering Regular Expressions is indeed an excellent book. 

I just wanted to point out Thunar&#039;s Bulk Rename extension (install using apt-get install thunar). &lt;a href=&quot;http://ubuntuforums.org/showpost.php?p=2906846&amp;postcount=10&quot; rel=&quot;nofollow&quot;&gt;In this forum thread over at the Ubuntu Forums&lt;/a&gt; I explain how to use nautilus-actions to be able to use it from the context menu within Nautilus.

&lt;a href=&quot;http://thunar.xfce.org/images/bulk-rename-search-and-replace.png&quot; rel=&quot;nofollow&quot;&gt;See this screenshot for a look at Bulk Rename&lt;/a&gt;.

*drool* I love GUIs...</description>
		<content:encoded><![CDATA[<p>Mastering Regular Expressions is indeed an excellent book. </p>
<p>I just wanted to point out Thunar's Bulk Rename extension (install using apt-get install thunar). <a href="http://ubuntuforums.org/showpost.php?p=2906846&amp;postcount=10" rel="nofollow">In this forum thread over at the Ubuntu Forums</a> I explain how to use nautilus-actions to be able to use it from the context menu within Nautilus.</p>
<p><a href="http://thunar.xfce.org/images/bulk-rename-search-and-replace.png" rel="nofollow">See this screenshot for a look at Bulk Rename</a>.</p>
<p>*drool* I love GUIs...</p>
]]></content:encoded>
	</item>
</channel>
</rss>
