开发者

Filename extraction with regex

I need to be able to only extract the filename (info.txt) from a line like:

07/01/2010  07:25p                 953 info.txt

I've tried using this: /d+\s+\d+\s+\d+\s+(?.?)开发者_如何学编程/, but it doesn't seem to work ...


How about

/\S+$/

I.e. the longest possible string of non-whitespace at the end of the line. (Hard to know for sure without more info about the possible inputs.)

As @J V pointed out, filenames with spaces in them (like his username) will not be parsed properly by the above regexp. We don't know from the question whether that's possible.

But I have a suspicion that we're looking at the output of Windows DIR command, or something very similar. In that case, the most reliable approach might be just to hack off the first 39 characters and keep the rest:

/^.{39}(.+)$/

Then $1 will contain the filename.

Better option:

But if you are using Windows DIR (as per your new comment), and you can control the DIR command, try

DIR /b

which removes the unneeded cruft (assuming you don't need the date, size etc.) and gives you one filename per line.

OK, you're using a Unix dir (per newer comment). The CentOS dir I have outputs one file per line, nothing else, when you give it no command line options. Chances are very good that whichever dir you're using can be persuaded to output filenames like that... then you wouldn't have to worry about using a regex that may or may not be correct for every possible input. Try man dir or dir --help to find out what command-line options to use.


\d\d:\d\d\w\s+\d+\s+(.*?)$

$1 will be the file name

The problem with your original regex is that it forgets the special characters :, /, and (?.?) means nothing...


Assuming that the files have extension as .txt you can try.

  (?<=(\s)*)\w*.txt


Why not just use the following regex:

\w+\.\w+
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜