1

I have a text file in which each line contains two "words" like this:

"(a+p(a|u)*h(a|u|i)*m)" "apehem"
"(a+p(a|u)*h(a|u|i)*a)" "correct"
"(a+p(a|u)*h(a|u|i)*e)" "correct"

First "word" is a regular expression pattern, second "word" is a real word. Both are double-quoted.

I want to search richTextBox3 for matches of first "word" of each line from the above file and replace each match with second "word".

I tried this (see below), but there is some error...

System.IO.StreamReader file = new System.IO.StreamReader(@"d:\test.txt"); 

string Word1="";
string Word2="";

lineWord1 = file.ReadToEnd().Split(" ");  //Error 
string replacedWord = Regex.Replace(richTextBox3.Text, Word1, Word2, 
  RegexOptions.IgnoreCase);

richTextBox3.Text = replacedWord;

Please advise. Thank you in advance!

Ωmega
  • 42,614
  • 34
  • 134
  • 203
Nqkoi
  • 57
  • 1
  • 7

3 Answers3

0

Try processing your file a line at a time.

System.IO.StreamReader file = new System.IO.StreamReader(@"d:\test.txt");

while (file.EndOfStream != true)
{
    //This will give you the two words from the line in an array
    //note that this counts on your file being perfect. You should probably check to make sure that the line you read in actually produced two words.
    string[] words = file.ReadLine().Split(' ');
    string replacedWord = Regex.Replace(richTextBox3.Text, words[0], words[1], RegexOptions.IgnoreCase);
    richTextBox3.Text = replacedWord;
}

Since you mentioned in a previous comment that this is going to be a spell checker, may I point out this link to you? https://stackoverflow.com/a/4912071/934912

Community
  • 1
  • 1
StillLearnin
  • 1,391
  • 15
  • 41
0

Be sure your file is saved with unicode encoding.

This solution should work for you >>

System.IO.StreamReader file = new System.IO.StreamReader(@"d:\test.txt");      
while (file.EndOfStream != true)      
{
  string s = file.ReadLine();
  Match m = Regex.Match(s, "\"([^\"]+)\"\\s+\"([^\"]+)\"", RegexOptions.IgnoreCase);
  if (m.Success) {
    richTextBox3.Text = Regex.Replace(richTextBox3.Text, 
      "\\b" + m.Groups[1].Value + "\\b", m.Groups[2].Value);
  }
}
Community
  • 1
  • 1
Ωmega
  • 42,614
  • 34
  • 134
  • 203
-1

I didn't know what was your reference to richTextBox3 but try this (untested, it's only to given you the way I'll try to solve):

var lines = System.IO.File.ReadAllLines(@"d:\test.txt");

foreach (var line in lines)
{
    var words = line.Split(' ');
    if (words.Length > 1)
        words[0] = words[1];
}

richTextBox3.Text = string.Join(Environment.NewLine, lines);

Be careful that you load all the file in memory, so don't do this if your file is big.

Fabske
  • 2,106
  • 18
  • 33
  • My file will be more than 500MB, I guess. And it's necessary to use regex, because the first word is actually a regular expression. – Nqkoi Oct 20 '12 at 13:55