Solved

Pattern matching in text files

Posted on 2003-10-30
13
266 Views
Last Modified: 2010-04-16
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
Comment
Question by:jon6
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
  • 2
13 Comments
 
LVL 1

Author Comment

by:jon6
ID: 9651448
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
 
LVL 9

Expert Comment

by:msdixon
ID: 9652402
0
 
LVL 1

Author Comment

by:jon6
ID: 9652572
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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 1

Author Comment

by:jon6
ID: 9656997
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
 
LVL 10

Expert Comment

by:ptmcomp
ID: 9664335
Can you give me something like a before and after situation. I have a hard time imaging what you want to do.
0
 
LVL 1

Author Comment

by:jon6
ID: 9665490
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
 
LVL 10

Expert Comment

by:ptmcomp
ID: 9665821
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
 
LVL 1

Author Comment

by:jon6
ID: 9665879
Could i do it with string.Replce, do you know how exactly i could do this with regular expression
0
 
LVL 10

Expert Comment

by:ptmcomp
ID: 9666215
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
 
LVL 1

Author Comment

by:jon6
ID: 9666263
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
 
LVL 10

Accepted Solution

by:
ptmcomp earned 210 total points
ID: 9667066
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
 
LVL 1

Author Comment

by:jon6
ID: 9670273
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
 
LVL 9

Assisted Solution

by:msdixon
msdixon earned 30 total points
ID: 9671027
\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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question