Parth Desai
Parth Desai

Reputation: 209

Get Text From file C#

I am reading text file line By line and in that I want to get data between special characters after checking whether line containing special character or not.In my case I want to check whether line contains <#Tag()> and if it contains then fetch the string between () i.e. line is having <#Tag(param1)> then it should return param1

But the problem is line may contains more then one <#Tag()> For Example Line is having - <#Tag(value1)> <#Tag(value2)> <#Tag(value3)> Then it should return first value1 then value2 and then value3

string contents = File.ReadAllText(@"D:\Report Format.txt");
int start = contents.IndexOf("Header") + "Header".Length;
int end = contents.IndexOf("Data") - "Header".Length;
int length = end - start;
string headerData = contents.Substring(start, length);
headerData = headerData.Trim(' ', '-');
MessageBox.Show(headerData);
using (StringReader reader = new StringReader(headerData))
{
    string line;
    while ((line = reader.ReadLine()) != null)
    {
        if (line.Contains("<#Tag"))
        {
            string input = line;
            string output = input.Split('<', '>')[1];
            MessageBox.Show(output);
            Globals.Tags.SystemTagDateTime.Read();
            string newoutput =  Globals.Tags.SystemTagDateTime.Value.ToString();
            input = input.Replace(output, newoutput);
            input = Regex.Replace(input, "<", "");
            input = Regex.Replace(input, ">", "");
            MessageBox.Show(input);
        }
    }
}

Upvotes: 3

Views: 245

Answers (4)

DDan
DDan

Reputation: 8276

You can also collect them after splitting the string by the constant values <#Tag( and )> like this:

string str = "<#Tag(value1)> <#Tag(value2)>  <#Tag(value3)>";
string[] values = str.Split(new string[] { "<#Tag(", ")>" }, StringSplitOptions.RemoveEmptyEntries);

values contains:

value1, value2, value3

Show the results in MessageBox:

foreach (string val in values) {
    if (!(String.IsNullOrEmpty(val.Trim()))) {
        MessageBox.Show(val);
    }
}

Edit based on you comment:

Can i display complete value1 value2 value3 in one message box not with comma but with the same spacing as it was

string text = "";
foreach (string val in values) {
    text += val;
}
MessageBox.Show(text);

Based on the comment: Now the last query Before showing it in message box I want to replace it by thier values for example 10 20 and 30

string text = "";
foreach (string val in values) {
   // where val is matching your variable (let's assume you are using dictionary for storing the values)
   // else is white space or other... just add to text var.
   if (yourDictionary.ContainsKey(val)) {
       text += yourDictionary[val];
   } else {
       text += val;
   }
}
MessageBox.Show(text);

Upvotes: 0

tchelidze
tchelidze

Reputation: 8308

Try following

var matches = Regex.Matches(line, @"(?<=\<\#Tag\()\w+(?=\)\>)")
foreach (Match match in matches)
  MessageBox.Show(match.Value);

If you want to accomplish context described in comments try following.

  var line = "<#Tag(value1)> <#Tag(value2)>  <#Tag(value3)>";
  var matches = Regex.Matches(line, @"(?<=\<\#Tag\()\w+(?=\)\>)");
  //use matches in your case to find values. i assume 10, 20 , 30
  var values = new Dictionary<string, int>() { { "value1", 10 }, { "value2", 20 }, { "value3", 30 } };
  const string fullMatchRegexTemplate = @"\<\#Tag\({0}\)\>";
  foreach (var value in values)
    Regex.Replace(line, string.Format(fullMatchRegexTemplate, value.Key), value.Value.ToString());

Upvotes: 3

Adam Tuliper
Adam Tuliper

Reputation: 30152

You could do this with a regex (I'll work on one)- but as a simple shortcut just do:

var tags  = line.Split(new string[] { "<#Tag" }, StringSplitOptions.None);
foreach(var tag in tags)
{
 //now parse each one
}

I see tchelidze just posted regex that looks pretty good so I'll defer to that answer as the regex one.

Upvotes: 0

Mohit S
Mohit S

Reputation: 14044

This might do the trick for you

[^a-zA-Z0-9]

Basically it matches all non-alphanumeric characters.

    private void removeTag()
    {
        string n = "<#Tag(value1)> <#Tag(value2)>  <#Tag(value3)>";
        string tmp = Regex.Replace(n, "Tag+", "");
        tmp = Regex.Replace(tmp, "[^0-9a-zA-Z]+", ",") ;
    }

Another one could be

string tmp = Regex.Replace(n, "[^0-9a-zA-Z]*[Tag]*[^0-9a-zA-Z]", ",");

Upvotes: 0

Related Questions