2

In strings like this (I get strings from Directory.GetFiles())

string temp = "\\folder_name\\file_name.filetype.[somename@somedomain].wallet"

What is the best way to substring: file_name.filetype

I could do something like this:

const string source = ".[somename@somedomain].wallet";
temp.Substring(0, temp.IndexOf(source, StringComparison.Ordinal));

... but problem is that "mail" in string ".[xxxx@xxxx].wallet" is changing, in my words my string source should be something like this:

const string source = ".[*].wallet"; //so all strings that are in .[all_strings].wallet

Is there an easy way to do something like this (with asterisk "*"), or I will have to substring piece by piece and concatenate this new string?

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
DaniKR
  • 2,418
  • 10
  • 39
  • 48

3 Answers3

2

You can construct a regex that requires a backslash before the substring of interest, and a text in square brackets followed by .wallet at the end.

Here is how you can do with in C# regex APIs:

string temp = @"\folder_name\file_name.filetype.[somename@somedomain].wallet";
var m = Regex.Match(temp, @"(?<=\\)[^.]*\.[^.]*(?=\.\[[^\]]*\].wallet)");
if (m.Success) {
    Console.WriteLine(m.Value);
} else {
    Console.WriteLine("<no match>");
}

Demo.

(?<=...) and (?=...) constructs are zero-length look-ahead and look-behind. They are not included in the m.Value.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

You could search for the 2nd index of . and take everything before that point.

string temp = "\\folder_name\\file_name.filetype.[somename@somedomain].wallet";

var filename = Path.GetFileName(temp);
var lastIndex = filename.IndexOf('.', filename.IndexOf('.') + 1);
var fileYouAreLookingFor = filename.Substring(0, lastIndex);

Working fiddle

Igor
  • 60,821
  • 10
  • 100
  • 175
  • 1
    I don't mind being corrected or downvoted but I do appreciate a comment on why the answer was downvoted. – Igor May 09 '17 at 20:36
  • I don't know who or why you have been downvoted, or how is this question duplicated, I can find from Peter Duhino questions where is duplicated, but from your answer I can find right solution, because my strings are exactly finished with ".[XXXX@XXX.com].wallet so correct, for me this is an answer of my problem – DaniKR May 09 '17 at 20:44
  • @DaniKR - also feel free to up-vote any answers that you feel helped you get to the correct solution. – Igor May 09 '17 at 20:47
  • thanks for answer, my upwote, but answer from dasblinkenlight is what is I was looking for, but still thanks for your answer – DaniKR May 09 '17 at 20:59
0

You could also use an regex to achieve this. The first group of the following one should be what you are looking for.

string temp = "\\folder_name\\file_name.filetype.[somename@somedomain].wallet";

var filenameRegex = new Regex("^.*\\\\(.*)\\.\\[.*\\]\\.wallet$");
var match = filenameRegex.Match(temp);

var result = match.Groups[1];
NtFreX
  • 10,379
  • 2
  • 43
  • 63