Solved

Pattern matching in text files

Posted on 2003-10-30
13
270 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

627 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