I am making editor for text file in C#.Net where i
have to edit the original text with some addition of symbol.
For example,
If line contains strings as Apartments and next line is started with apartment number then, i have to insert ^ symbol in every string start with apartment
number.
string s = @"1405 Wagner Apartments
1407 Wagner Apartments
1 Sarver Frank L
2 Bard Arthur M
8 McMullan Eliz
4 Morris Jack R
5 Stephens Stanley C
1406 Klitch Minnie Mrs";
string output;
StringBuilder b = new StringBuilder();
StringReader r = new StringReader(s);
while (r.Peek() >0 )
{
string line = r.ReadLine();
if (Regex.IsMatch(line, "Apartments"))
b.AppendLine(line);
else
b.AppendLine (Regex.Replace (line,"^\\d+","^$0^"));
}
output = b.ToString();
Regards,
Stefan Uzunov
MCTS: .NET Framework 3.5 ASP.NET Applications
But the problem is if previous line contains apartments and next line are started with the apartment no. then i have to add the symbol.
Untill i got the street number (for ex, 1406 or 1408)
Consider this line 1407 Wagner Apartments where
1407 is street no. & having word apartments.And after this the apartment no. line are started untill we get the line started with street number i.e 1406 Klitch Minnie
Mrs i have to put symbol.
string s = @"1405 Wagner Apartments
1407 Wagner Apartments
1 Sarver Frank L
2 Bard Arthur M
8 McMullan Eliz
4 Morris Jack R
5 Stephens Stanley C
1406 Klitch Minnie Mrs";
string output;
Boolean ApartmentFound = false;
StringBuilder b = new StringBuilder();
StringReader r = new StringReader(s);
while (r.Peek() > 0)
{
string line = r.ReadLine();
if (Regex.IsMatch(line, "^\\d{4}")) ApartmentFound = false;
if (Regex.IsMatch(line, "Apartments")) ApartmentFound = true;
if (ApartmentFound)
{
b.AppendLine(Regex.Replace(line, "^(\\d{1,2})\\s", "^$1^"));
}
else
{
b.AppendLine(line);
}
}
output = b.ToString();
Regards,
Stefan Uzunov
MCTS: .NET Framework 3.5 ASP.NET Applications
1321 Brame Lester E gas sta
Fourteenth st crosses
1400 Grimes Wm L barber
1401 Hoerner May I nurse
Hoerner Wm M
Hoerner Wm S
1402 Creager Braton R
1403 Hoerner Apartments
1404 Wagner Apartments
1 Sarver Frank L
2 Bard Arthur M
3 McMullan Eliz
4 Morris Jack R
5 Stephens Stanley C
1405 Klitch Minnie Mrs
Please reply how to get this type of output from above input lines:
~1321~|Brame Lester E gas sta|
Fourteenth st crosses
~1400~|Grimes Wm L barber|
~1401~|Hoerner May I nurse|
>Hoerner Wm M>
>Hoerner Wm S>
~1402~|Creager Braton R|
~1403~|Hoerner Apartments|
~1404~|Wagner Apartments|
^1^|Sarver Frank L|
^2^|Bard Arthur M|
^3^|McMullan Eliz|
^4^|Morris Jack R|
^5^|Stephens Stanley C|
~1405~|Klitch Minnie Mrs|
string s = @"1321 Brame Lester E gas sta
Fourteenth st crosses
1400 Grimes Wm L barber
1401 Hoerner May I nurse
Hoerner Wm M
Hoerner Wm S
1402 Creager Braton R
1403 Hoerner Apartments
1404 Wagner Apartments
1 Sarver Frank L
2 Bard Arthur M
3 McMullan Eliz
4 Morris Jack R
5 Stephens Stanley C
1405 Klitch Minnie Mrs";
string output;
StringBuilder b = new StringBuilder();
StringReader r = new StringReader(s);
while (r.Peek() > 0) b.AppendLine(Regex.Replace(r.ReadLine(), "^(\\d+)\\s(.+)$", "~$1~|$2|"));
output = b.ToString();
Regards,
Stefan Uzunov
MCTS: .NET Framework 3.5 ASP.NET Applications
Specially the regular exp.So that i can made changes as per requirement.
Below is some sample input:
************INPUT***********
1 Brame Lester E gas sta
Fourteenth st crosses
14 Wagner Apartments
1407 Wagner Apartments
1 Sarver Frank L
2 Bard Arthur M
8 McMullan Eliz
4 Morris Jack R
5 Stephens Stanley C
1406 Klitch Minnie Mrs
Hill Restaurant
Endress Matthew H
rear Bricker Jack M
Grace st ends
MARKET SQUARE S (Buffalo) from 201 Market south to Blackberry, wd 3
MARKET SQUARE N from 200 Market north to Strawberry, wd 2
ZIP CODE 31204
************OUTPUT**************
~1~|Brame Lester E gas sta|
Fourteenth st crosses
~14~|Wagner Apartments|
~1407~|Wagner Apartments|
^1^|Sarver Frank L|
^2^|Bard Arthur M|
^3^|McMullan Eliz|
^4^|Morris Jack R|
^5^|Stephens Stanley C|
~1406~|Klitch Minnie Mrs|
>Hill Restaurant>
>Endress Matthew H>
Eighteenth st crosses
>rear>|Bricker Jack M|
$Grace st ends$
<MARKET SQUARE S<@(Buffalo)@from 201 Market south to Blackberry, wd 3
<MARKET SQUARE N<from 200 Market north to Strawberry, wd 2
ZIP CODE#31204#
Please consider that street number is not fixed.So this line will not work.
Specially the regular exp.So that i can made changes as per requirement.
Regular expressions provide a powerful, flexible, and efficient method for processing text. The extensive pattern-matching notation of regular expressions enables you to quickly parse large amounts of text to find specific character patterns; to validate
text to ensure that it matches a predefined pattern (such as an e-mail address); to extract, edit, replace, or delete text substrings; and to add the extracted strings to a collection in order to generate a report. For many applications that deal with strings
or that parse large blocks of text, regular expressions are an indispensable tool.
More stuffs about Regular Expression please check this link:
Anuj_SwDevel...
Member
521 Points
140 Posts
Replace string in text file.
Jul 27, 2011 09:21 AM|LINK
Hi,
I am making editor for text file in C#.Net where i have to edit the original text with some addition of symbol.
For example,
If line contains strings as Apartments and next line is started with apartment number then, i have to insert ^ symbol in every string start with apartment number.
---------Input---------
1405 Wagner Apartments
1407 Wagner Apartments
1 Sarver Frank L
2 Bard Arthur M
8 McMullan Eliz
4 Morris Jack R
5 Stephens Stanley C
1406 Klitch Minnie Mrs
-----------------------
--------Output---------
1405 Wagner Apartments
1407 Wagner Apartments
^1^|Sarver Frank L|
^2^|Bard Arthur M|
^8^|McMullan Eliz|
^4^|Morris Jack R|
^5^|Stephens Stanley C|
1406 Klitch Minnie Mrs
-----------------------
Pls provide some logic to do this.
Steelymar
All-Star
15283 Points
2239 Posts
Re: Replace string in text file.
Jul 27, 2011 09:43 AM|LINK
"1406 Klitch Minnie Mrs" haven't "Apartments" but in output is not with ^1406 ^ ?
Stefan Uzunov
MCTS: .NET Framework 3.5 ASP.NET Applications
Steelymar
All-Star
15283 Points
2239 Posts
Re: Replace string in text file.
Jul 27, 2011 09:53 AM|LINK
use:
string s = @"1405 Wagner Apartments 1407 Wagner Apartments 1 Sarver Frank L 2 Bard Arthur M 8 McMullan Eliz 4 Morris Jack R 5 Stephens Stanley C 1406 Klitch Minnie Mrs"; string output; StringBuilder b = new StringBuilder(); StringReader r = new StringReader(s); while (r.Peek() >0 ) { string line = r.ReadLine(); if (Regex.IsMatch(line, "Apartments")) b.AppendLine(line); else b.AppendLine (Regex.Replace (line,"^\\d+","^$0^")); } output = b.ToString();Stefan Uzunov
MCTS: .NET Framework 3.5 ASP.NET Applications
Anuj_SwDevel...
Member
521 Points
140 Posts
Re: Replace string in text file.
Jul 27, 2011 10:11 AM|LINK
Hi, Stefan
Thanks for reply.
But the problem is if previous line contains apartments and next line are started with the apartment no. then i have to add the symbol.
Untill i got the street number (for ex, 1406 or 1408)
Consider this line 1407 Wagner Apartments where 1407 is street no. & having word apartments.And after this the apartment no. line are started untill we get the line started with street number i.e 1406 Klitch Minnie Mrs i have to put symbol.
Pls help.
Steelymar
All-Star
15283 Points
2239 Posts
Re: Replace string in text file.
Jul 27, 2011 10:38 AM|LINK
try:
string s = @"1405 Wagner Apartments 1407 Wagner Apartments 1 Sarver Frank L 2 Bard Arthur M 8 McMullan Eliz 4 Morris Jack R 5 Stephens Stanley C 1406 Klitch Minnie Mrs"; string output; Boolean ApartmentFound = false; StringBuilder b = new StringBuilder(); StringReader r = new StringReader(s); while (r.Peek() > 0) { string line = r.ReadLine(); if (Regex.IsMatch(line, "^\\d{4}")) ApartmentFound = false; if (Regex.IsMatch(line, "Apartments")) ApartmentFound = true; if (ApartmentFound) { b.AppendLine(Regex.Replace(line, "^(\\d{1,2})\\s", "^$1^")); } else { b.AppendLine(line); } } output = b.ToString();Stefan Uzunov
MCTS: .NET Framework 3.5 ASP.NET Applications
Anuj_SwDevel...
Member
521 Points
140 Posts
Re: Replace string in text file.
Jul 27, 2011 01:01 PM|LINK
Hi,Stefan
Problem is solved by your post.
Please reply how to get this type of output from above input lines:
Steelymar
All-Star
15283 Points
2239 Posts
Re: Replace string in text file.
Jul 27, 2011 01:20 PM|LINK
string s = @"1321 Brame Lester E gas sta Fourteenth st crosses 1400 Grimes Wm L barber 1401 Hoerner May I nurse Hoerner Wm M Hoerner Wm S 1402 Creager Braton R 1403 Hoerner Apartments 1404 Wagner Apartments 1 Sarver Frank L 2 Bard Arthur M 3 McMullan Eliz 4 Morris Jack R 5 Stephens Stanley C 1405 Klitch Minnie Mrs"; string output; StringBuilder b = new StringBuilder(); StringReader r = new StringReader(s); while (r.Peek() > 0) b.AppendLine(Regex.Replace(r.ReadLine(), "^(\\d+)\\s(.+)$", "~$1~|$2|")); output = b.ToString();Stefan Uzunov
MCTS: .NET Framework 3.5 ASP.NET Applications
Anuj_SwDevel...
Member
521 Points
140 Posts
Re: Replace string in text file.
Jul 28, 2011 06:44 AM|LINK
Hi, Stefan
Thanks for the reply it helped me a lot.
Can you please explain this line:
Specially the regular exp.So that i can made changes as per requirement.
Below is some sample input:
Anuj_SwDevel...
Member
521 Points
140 Posts
Re: Replace string in text file.
Jul 28, 2011 01:01 PM|LINK
Hi,
Please some one help !!!
To complete my project.
Mamba Dai - ...
All-Star
23530 Points
2683 Posts
Microsoft
Re: Replace string in text file.
Aug 02, 2011 08:28 AM|LINK
Hi,
Regular expressions provide a powerful, flexible, and efficient method for processing text. The extensive pattern-matching notation of regular expressions enables you to quickly parse large amounts of text to find specific character patterns; to validate text to ensure that it matches a predefined pattern (such as an e-mail address); to extract, edit, replace, or delete text substrings; and to add the extracted strings to a collection in order to generate a report. For many applications that deal with strings or that parse large blocks of text, regular expressions are an indispensable tool.
More stuffs about Regular Expression please check this link:
http://msdn.microsoft.com/en-us/library/hs600312.aspx
Feedback to us
Develop and promote your apps in Windows Store