Regex pattern matching C#

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


Regex pattern matching C#



[Here is the picture]1I want to loop through each file in a folder and save the substring of the file name to a variable.



For example lets say I have the following file in a folder:



ERISRequest_INC1234567.csv //Should Print -> INC1234567



ERISRequest_INC8901234.csv //Should print -> INC8901234



fileName.csv //should skip this one



I want to extract the shown substring if and only if the file name starts with ERISRequest_ and store it in a variable


using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FileName
{
class Program
{
static void Main(string args)
{
DirectoryInfo di = new DirectoryInfo(@"C:");
foreach(FileInfo fi in di.GetFiles())
{
Console.WriteLine(fi.Name);

}
Console.ReadLine();
}
}
}





if (Path.GetFileNameWithoutExtension(fi.Name).StartsWith("ERISRequest_")) {...}
– Dmitry Bychenko
6 hours ago


if (Path.GetFileNameWithoutExtension(fi.Name).StartsWith("ERISRequest_")) {...}





To follow on from what Dmitry said, you can then use Path.GetFileNameWithoutExtension to deal with the file extension
– Dan
6 hours ago





It doesnt print it on the console I add the condition inside the for each loop
– whatever11938
6 hours ago





@whatever11938: what are the files' names printed out without any condition, please?
– Dmitry Bychenko
5 hours ago





It does not print anything, the code I posted prints all file names
– whatever11938
5 hours ago




1 Answer
1



It helps you. Is that what you're looking for?


var regstr = $"^ERISRequest_.*?";

DirectoryInfo di = new DirectoryInfo(@"C:");
foreach (FileInfo fi in di.GetFiles())
{
string name = Path.GetFileNameWithoutExtension(fi.Name);
if (Regex.IsMatch(name, regstr))
{
Console.WriteLine(name.Replace("ERISRequest_", ""));
}
}



An example for that;


var regstr = $"^ERISRequest_.*?";

var result = Regex.IsMatch("ERISRequest_.", regstr); // true
var result2 = Regex.IsMatch("ERISRequest", regstr); // false
var result3 = Regex.IsMatch("aERISRequest_", regstr); // false
var result4 = Regex.IsMatch("ERISRequest_asd", regstr); // true



ERISRequest_INC8901234.csv file



Result on console



Code





doesnt seem to print anything
– whatever11938
5 hours ago





There is no reason for it is not working i think. I runned on my computer an it worked. Are there any error or etc..
– Waayd
4 hours ago





that is not what I want... I tried again and it doesnt work
– whatever11938
4 hours ago





@whatever11938 is it possible there is no file to match with regstr?
– Waayd
4 hours ago


regstr





Here is an example of what I want: ERISRequest_INC1234567.csv //Should Print -> INC1234567 ERISRequest_INC8901234.csv //Should print -> INC8901234 fileName.csv //should skip this one
– whatever11938
4 hours ago








By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Makefile test if variable is not empty

Will Oldham

Visual Studio Code: How to configure includePath for better IntelliSense results