Solved

Pattern matching in text files

Posted on 2003-10-30
13
248 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
  • 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
 
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

706 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now