0

Goal:
Make sure that the data1 and all the way to data31 is correct and data4 and all the way to data7 is error with support of regex

 Regex regexp = new Regex(@"^[a-zA-Z]:\\.*$");

in C#.

Problem:
data6 and data7 is supposed to be error but instead it is correct.

Don't know how to solve it.

    Regex regexp = new Regex(@"^[a-zA-Z]:\\.*$");

    //Correct result
    string data1 = "C:\\33\\dd\\Desktop\\151222055438.txt";
    string data2 = "c:\\ff\\ded\\Desktop\\151222055438.txt";
    string data3 = "Z:\\ss\\gg\\Desktop\\151222055438.txt";
    string data31 = "d:\\da\\ds\\Df\\ff.txt";

    //Error result
    string data4 = "3:\\rr\\555\\Desktop\\151222055438.txt";
    string data5 = "d\\33\\4\\Desktop\\151222055438.txt";
    string data6 = "s:\\\\rr\\d\\Desktop\\151222055438.txt";
    string data7 = "s:\\\\\\f\\f\\Desktop\\151222055438.txt";


    if (regexp.Match(data1).Success)
    {
        int correct = 23;
    }

    if (regexp.Match(data2).Success)
    {
        int correct = 23;
    }

    if (regexp.Match(data3).Success)
    {
        int correct = 23;
    }

    if (regexp.Match(data31).Success)
    {
        int correct = 23;
    }



    if (regexp.Match(data4).Success)
    {
        int error = 23;
    }

    if (regexp.Match(data5).Success)
    {
        int error = 23;
    }

    if (regexp.Match(data6).Success)
    {
        int error = 23;
    }

    if (regexp.Match(data7).Success)
    {
        int error = 23;
    }

    int sds = 23;
Alan Moore
  • 73,866
  • 12
  • 100
  • 156
HelloWorld1
  • 13,688
  • 28
  • 82
  • 145
  • It looks like you are validating filenames, why not use https://stackoverflow.com/questions/422090/in-c-sharp-check-that-filename-is-possibly-valid-not-that-it-exists instead? – Nameless One Dec 29 '15 at 08:29

3 Answers3

1

I made a slight modification to your expression so that it becomes as follows: ^[a-zA-Z]:(\\{2}[a-zA-Z0-9.]+)+$ (example available here).

In your case, you seem to be looking explicitly for pairs of \ characters, which is what the \\{2} does. Also, between the pairs of \ you seem to only be accepting letters and numbers. At the end, you seem to be also looking for period characters.

npinti
  • 51,780
  • 5
  • 72
  • 96
1

This problem was solved in an answer to your original question. You just have to adapt it to work with single instead of double backslashes:

var regexp = new Regex(@"^[a-zA-Z]:(\\[^\\]+)+$");

\\ matches a single backslash (because \ is not special in a verbatim string literal, but it is special in a regex). Then [^\\]+ matches one or more of any characters except backslash; that's the directory or file name.

Community
  • 1
  • 1
Alan Moore
  • 73,866
  • 12
  • 100
  • 156
0

Try this

Regex regexp = new Regex(@"^[a-zA-Z]:(\\{2}[a-zA-Z\d]+)+(\\{2}[\d]+)*.txt$");

IF your files have ".txt" extension and have only digits in name, this might work even better.

The reason for two slashes "\\" is to indicate backslash. as it will consider single backslash as escape character.

Maulik Modi
  • 1,205
  • 12
  • 22