Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 283
  • Last Modified:

Pattern matching in text files

Is it possible to find a whole paragraph in a text file and replace it, i have looked at the msdn and cant figure out how to do it

I have various paragraphs that can be edited through a windows form, they include {} , = in the paragraphs.

Is there a way to find a set paragraph say

@living,
Jon = {live},
at = {home},
with = {wife}
}

within a text file and replace it with one already in the program called newfile


Any code or help would be great
0
jon6
Asked:
jon6
  • 7
  • 4
  • 2
2 Solutions
 
jon6Author Commented:
Say the text file is called questions.txt

it must delete what it finds already in the text file then add the variable newfile into the text file
0
 
msdixonCommented:
0
 
jon6Author Commented:
Has anyone got some code that they could change and post up for me, if anyone can give me full working code i will increase the points by 50
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
jon6Author Commented:
The stuff i want to delete in the text file will be in a variable called deletefile

i dont need to know how to add another variable into the text file now i have figured it out.


All i need to know the code for is to search through the text file question.txt and find the contents of the variable deletefile, it will look something like

@living,
Jon = {live},
at = {home},
with = {wife}
}


this but will change each time so cant be fixed.


I am increasing the points to 225
0
 
ptmcompCommented:
Can you give me something like a before and after situation. I have a hard time imaging what you want to do.
0
 
jon6Author Commented:
This is the way i am trying it

System.IO.StreamReader myFile =
                        new System.IO.StreamReader("questions.tex");
                  string myString = myFile.ReadToEnd();

                  myFile.Close();


                  string jon = this.richTextBox1.Text;

                  string strData = Regex.Replace(myString, file, jon);



I read in the whole text file into a variable i then get a value from a list view from a different class that is called file, then the bit i have editted is jon the contents of the richtextbox, however when i try it strData does not change.
0
 
ptmcompCommented:
You missunderstood the way regular expression work. Usually you have a search expression and a replace expression. These expressions are interpreted and contain logic - they are not just strings.
Regex.Replace is not the same as string.Replace
0
 
jon6Author Commented:
Could i do it with string.Replce, do you know how exactly i could do this with regular expression
0
 
ptmcompCommented:
Use string.Replace to replace a string that you know exactly and entierly with a different string.
Use Regex to match (and if you want replace) strings that you know partially or you only know the pattern.

The pattern would describe:
@para_name,
item_name = {item_value},
...
}

Now we have to translate this in a regular expression and you need to know what you want to match. Is it the whole paragraph or an inner item?
0
 
jon6Author Commented:
The text file will contain stuff like this, but they will all be different, i am loading it into a treeview then using a form to edit it so their will be one variable called oldfile, which i will need to search for in the text file and replace with the edited variable called edited.

So the thing we will need to patch will be different each time. I thought i could load the hwole contents of the text file into a variable first then search for the old one replace it with the new one and then write it to the same text file

@living,
Jon = {live},
at = {home},
with = {wife}
}
@living,
Jon = {live},
at = {home},
with = {wife}
}
@living,
Jon = {live},
at = {home},
with = {wife}
}
0
 
ptmcompCommented:
If you know the exact string you want to replace then use:
newfile = oldfile.Replace(old, edited);

----

I found a good "regular experssion" editor: http://www.codeproject.com/dotnet/expresso.asp?print=true

Here is one poss. with regex:

string name = 'living';
newfile = Regex.Replace(oldfile, @"(^@" + name + "(.|\r|\n)*?\r\n})", newvalue, RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.ExplicitCapture);


0
 
jon6Author Commented:
I have managed to get the oldfile.Replace to work however the strings look like this

@living,\r\nJon = {live},\r\nat = {home},\r\nwith = {wife}\r\n}\r\n

Can you tell me what \r is i know \n is newline but it wont work without the \r in it, is there a way to remove \r from a string when you read it in
0
 
msdixonCommented:
\r is a carraige return
\n is a new line

they sound the same, but aren't.

it's in the syntax link i provided (a little reading goes a long way as far as understading any given subject):
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/jscript7/html/jsjsgrpregexpsyntax.asp
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 7
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now