0

我很讨厌正则表达式,但我需要一个能够理解文件格式的文件,其扩展名是: Anycharacter.threecharachter 扩展名 我可以使用. 如果编译器得到它

我试过

Regex ex = new regex(".*\\..*")

Regex ex = new regex(".*\\....");

应该通过的例子:

 ex.IsMatch("test.zip");   

感谢@Alice 的留言;我更改了文件浏览器现在显示的代码:-rwxr-x--- 1 root nto 1007 Jan 10 14:28 chat.sh

或者

drwxr-x--- 1 root nto 1007 Jan 10 14:28 个帐户

或者

lrwxrwxrwx 1 root nto 9 Jan 15 06:43 lib -> /base/lib

d 是在行首定义目录的内容。我想读这样的一句话并得到回报

布尔Isdirectory;

字符串 fName;

4

5 回答 5

1
[^\.]    //Not a period once
.*       //Anything 0..more times
\.       //Period
.{1,3}   //Anything, up to three times

All together:

[^\.].*\..{1,3}

If the entire line/text has to match, put it in ^$:

^[^\.].*\..{1,3}$

Note that files without extensions are also valid filenames, so you may need to relax the period . requirement at some point :) Plus, period matches symbols that aren't valid in a filename, like *, /, etc. so you may be asking the wrong question here.

EDIT: Now that you added the flags, the question makes more sense. It is very easy to do the new check:

if( line.StartsWith( "d" ) )
    return true; //It's a directory.
于 2013-01-31T18:24:33.067 回答
0

I guess this is what you are looking for

^.*\.[^.]{3}$

If you want to match a particular character n number of times you can use {} quantifier

so .{3} would match three characters

x{2} would match 2 x

x{1,5} would match 1 to 5 x

于 2013-01-31T18:20:46.463 回答
0

This is the file name Regex I use for windows file names

^[^/\\:*""<>|]+?\.\w{1,3}$

These symbols^/\:*"<>| represent the disallowed character for file names on windows.

If you dont want more than one period in your file name you can use this one:

^[^/\\:*""<>|.]+?\.\w{1,3}$
于 2013-01-31T19:23:47.363 回答
0
    string x = "drwx------+ 1 Administrators Domain Users        0 Nov 30  2011 VSWebCache";
    Match m = Regex.Match(x, @"^(?<dir>d)?.*?(?<name>\w+(?:\.\w{3})?)$");

    if(m.Success)
    {
        string name = m.Groups["name"].Value;
        bool isDir = m.Groups["dir"].Success;
    }

"name" is now a string containing the filename or directory name. "isDir" now contains a bool whether the listing starts with "d" for directory or not.

Your regex is as follows:

^(?<dir>d)?.*?(?<name>\w+(?:\.\w{3})?)$

It is saying this: Create 2 groups named "dir" and "name". If starts with "d" then group "dir" will contain "d". Group "name" will contain either the file name or directory name. There is some play in the name as it will match a "filename" and "filename.ext" if a "." exists in the name.

This will also work if

string x = @"-rwx------+ 1 Administrators Domain Users    72080 Dec 16  2011 g2mdlhlpx.exe"; 
于 2013-02-01T02:13:50.470 回答
-1

试试这个:

.+\.(.{1,3})+

.+ will look for file name, and will make sure that file name contains at least 1 character. \. will look for a period. .{1,3} will look for extension that could be one to three characters long. (.{1,3})+ will take care of file names that have extensions like filename.tar.gz.

so the regex will validate filename.z, filename.gz, filename.zip, filename.tar.gz, etc.

于 2013-01-31T18:19:44.790 回答