Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

match directory paths whose last name is numeric

Last post 01-28-2013, 4:47 PM by Aussie Susan. 3 replies.
Sort Posts: Previous Next
  •  01-24-2013, 11:56 AM 87228

    match directory paths whose last name is numeric

    I need an expression that will match a directory path only if the last directory name is all numeric [0-9]. I thought I knew how but I can't get anything to work. I know I need to anchor it to the end but I don't know how to get to the last / in the string. TIA.

     /*\/[0-9]?$/

    Something like that?

  •  01-24-2013, 5:58 PM 87229 in reply to 87228

    Re: match directory paths whose last name is numeric

    The key issue here is likely to be the regex variant that is being used and the various capabilities it may have. (Unfortunately regex engines are NOT all the same).

    I am assuming that you are using some file search tool that can filter file names based on some regex pattern. Therefore we really need to know what the tool is or, preferably, the regex engine that it uses.

    Further, I assume you are talking about "windows" style file names as in "MyWord.docx" and "Temp.002" etc., and that the tool is presenting the file name as text that contains *only* the file name and type (possibly with a preceding directory path but certainly nothing after the file type.

    In that case you are probably not far from the mark. Working backwards, the '$' is the usual zero-width anchor to match the end of the text (or the end of a line if the "multiline" option is set). With some environments, the end of line is taken as "\r\n" with the "$" matching the "\n" only - therefore you may need '\r?$' to anchor the end of the line.

    Moving back, you want to match a sequence of digits. The '[0-9]' character set will certainly match a digit (your regex may also accept '\d' as a shortcut way of expressing this character set definition). However I suspect that you have used the wrong quantifier: the '?' quantifier will match the previous item (the '[0-9]' in this case) zero or 1 time; whereas I suspect you mean the '+' quantifier which will match the previous item 1 or more times, matching as many times as possible.

    Therefore the end of the pattern might be:

    \d+$

    or

    \d+\r?$

    To make sure that the character before the first digit is a directory path separator, the  what you have is probably correct, namely '/' but as your regex environment seems to want the pattern  to be delimited and you have chosen the "/" as the delimiter, then you are correct to escape it to be "\/'. However you will need to check that the file name that is being checked will always have at least a partial path before it.

    I'm not sure the purpose of the '*' at the start of you pattern as most regex parsers would flag that up as a syntax error. (As I type this I wonder if this is supposed to be used as the input to some parser that treats '*' as match any character - this is why knowing the actual regex variant that is being used is so important).

    However, the way a regex tries to match a pattern to the text means that, if you don't care what comes before the part you have matched, then you don;t need to include it in the pattern. While the regex engine will start at the beginning of the text, the anchor etc. will make the match fail until it has progressed far enough through to only match the digits right at the end.

    Therefore the pattern we have come up with so far should be sufficient.

    Susan 

  •  01-25-2013, 8:26 AM 87232 in reply to 87229

    Re: match directory paths whose last name is numeric

    Thanks for the reply. I am using Linux and grep. Specially, perl's grep. I am processing only directory paths and want to filter only those whose lowest level are all numeric.

    I can accomplish it using brute force in a loop with 'looks_like_number()' but I'd prefer a more elegant solution using grep.

    I hope that helps.

     

  •  01-28-2013, 4:47 PM 87253 in reply to 87232

    Re: match directory paths whose last name is numeric

    I must admit that I thought the "perl" and 'grep" were two separate things until I looked it up just now. Therefore I must also assume that the "perl grep" operator uses standard perl regex pattern operators.

    Given all that, have you tried the pattern mentioned above, namely

    /\/\d+$/

    Without examples of the text file you are scanning it is a bit hard to test. However my test platform (not perl but the syntax used is *very* close) takes

    hello/ufo/1234
    /1234
    /abc
    /1234.321

    and matches the first 2 only.

    Susan 

View as RSS news feed in XML