Solved

Pattern matching in text files

Posted on 2003-10-30
13
255 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Check my code out(2) 2 54
Different Delete Messages 7 37
C#.NET and microsoft certification. 3 34
Windows Media Player 2 18
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

863 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

24 Experts available now in Live!

Get 1:1 Help Now