using Regex, I need to do the following:
Replacing * with X when found
omit / when found in
- _ are allowed
Upper case and Lower case are allowed
Numerical format and alphanumeric format are allowed
Do I need also to use RegexOptions.None instead of the following? I still cannot understand Regex very well :-( but I keep trying
Regex regex = new Regex("(?:[^]|(?<=['\"])s)", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.Compiled);
VulpesPosted Aug 21, 2013, 11:37 AM
You have essentially three separate operations here - two replacements and a match of the resulting string. The following code should achieve that:
However, you shouldn't need to use it much as most Regex methods will have overloads which don't require a RegexOptions parameter in any case.
Rano AHPosted Aug 22, 2013, 7:27 AM
I must be mistaken on this. My codes works perfectly :-), I just forgot that list index begins with [0] instead of [1]. So If I need to read line four, I just need to pass list[3] instead of list[4]. What a mistake :-)
Anyway, thanks for the help Vulpes, I found you guys on time all the time :-)
If I encounter some problems, for sure I will get back to you guys for the assistance :-)
VulpesPosted Aug 22, 2013, 6:02 AM
So, are you saying that you want it it to skip any line which begins with 0 or some other number or which otherwise contains alphanumeric characters?
If so, it would help if you could post a sample file so I can see exactly what needs to be skipped here.
Rano AHPosted Aug 22, 2013, 5:40 AM
ear Vulpes
Thank you for replying my question. Any one of you guys is welcome to answer my questions. :-)
See my code below please:
if (Files.Lenght > 0)
foreach (string file in files)
{
try
{
var list = new List
using (var reader = new StreamReader(file))
{
for (int i = 0; i < 10; i++)
{
list.Add(reader.ReadLine());
}
}
string newFileName = string.Concat(Path.GetFileNameWithoutExtension(file), "-",list[3], "-", list[8],"-",list[9],"-",list[10], "txt");
newFileName=Regex.Replace(newFileName, @"\*", "X");
newFileName = Regex.Replace(newFileName, @"\/", "");
string newFile = string.Concat(outputDirectory,"\\",newFileName,".txt");
File.Copy(file, newFile);
This code works fine, but still I face some problems in which I'm trying to solve:
When I read the file, it skips the rows which begins with 0, or number or if it contains alphanumeric characters. in this case it reads line 4 instead of three for instance and line 9 instead of 8 and so on.. This will give me incorrect file name