I'm trying to capture some information from some URLs so that I can store the downloaded data into directories that make some sense.

The type of URL's I'm downloading the data from are as follows:

http://www.domain.com/data/2005/sales/01012005.txt http://www.domain.com/data/2005/sales-jan/01232005.txt http://www.domain.com/data/2005/sales-local/01012005.txt http://www.domain.com/data/2005/sales-outside-jan/01012005.txt ... ...
What I want to extract from this is:

sales
sales-jan
sales-local
sales-outside-jan
...
...

The regex I have come up with to extract the information is:

$dir = $1 if /\/(\w+(|-\w+|-\w+-\w+))\/\w+\.txt$/;

This regex appears to be working correctly. My question is am I going about it the right way? Could I have shortened the regex somehow?

Thanks,
Mike

Update: Corrected typo in regex that GrandFather found.

Thanks GrandFather and ikegami for the suggestions. Yes, I should have used a different delimiter, the leaning toothpicks are confusing. I understand GrandFather's suggestion, but I'll have to study ikegami's suggestion a bit. Thanks!

Update II: Thanks to all for the great responses / ideas. Thanks to davidrw & YuckFoo for their suggestions on the split. Frankly I hadn't even thought of that. I became so wrapped up in the regex to get the directory, I hadn't even thought about the filename yet. Clearly a case of not seeing the forest for the trees.


In reply to Regex question by cajun

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.