Solved

string search and replace

Posted on 2011-02-13
11
280 Views
Last Modified: 2013-12-17
Hi experts,
I have following text which I need to remove tag <p> and also insert <BR /><BR /> for tag </p> at the end.

Thanks

<p style="margin-top: 0pt; margin-right: 0pt; margin-bottom: 0pt; margin-left: 0pt">kajf aksfja skfjas fkajsf asjf sakfjas fkasjf kasfj safjsinancial video content has posted the latest <a href="http://www.testh.com/media/763/Dynacor_Gold_Mines_TSX:_DNG_News_Alert/" target="_blank">video</a> "News Alert" for Dynacor Gold Mines Inc. If the link is unavailable, please visit <a href="http://www.kfjaskfjsaf.com/" target="_blank">www.Isfsf.com</a> and enter "Dynacor" in the search box.</p>

Open in new window

0
Comment
Question by:saloj
[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
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 12

Expert Comment

by:Praveen Kumar
ID: 34885809
string s = @"<p style='margin-top: 0pt; margin-right: 0pt; margin-bottom: 0pt; margin-left: 0pt'>kajf aksfja skfjas fkajsf asjf sakfjas fkasjf kasfj safjsinancial video content has posted the latest <a href='http://www.testh.com/media/763/Dynacor_Gold_Mines_TSX:_DNG_News_Alert/' target='_blank'>video</a> 'News Alert' for Dynacor Gold Mines Inc. If the link is unavailable, please visit <a href='http://www.kfjaskfjsaf.com/' target='_blank'>www.Isfsf.com</a> and enter 'Dynacor' in the search box.</p>";

s = s.Replace("<p", "").Replace("/p>", "");
s = s + "</BR></BR>";
0
 
LVL 2

Author Comment

by:saloj
ID: 34886069
it doesnot give me what i want, i want following text. also there are many similar <p  sfsfsaf asf> tags which I want to loop and delete.

kajf aksfja skfjas fkajsf asjf sakfjas fkasjf kasfj safjsinancial video content has posted the latest <a href='http://www.testh.com/media/763/Dynacor_Gold_Mines_TSX:_DNG_News_Alert/' target='_blank'>video</a> 'News Alert' for Dynacor Gold Mines Inc. If the link is unavailable, please visit <a href='http://www.kfjaskfjsaf.com/' target='_blank'>www.Isfsf.com</a> and enter 'Dynacor' in the search box.<BR /><BR />

Thanks
0
 
LVL 11

Expert Comment

by:SAMIR BHOGAYTA
ID: 34886180
Hi, use this code

private void textBox_Leave(object sender, System.EventArgs e)
{
    listBox1.Items.Add(((TextBox)sender).Text);
}

http://msdn.microsoft.com/en-us/library/fk49wtc1.aspx
0
Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

 
LVL 7

Assisted Solution

by:kemi67
kemi67 earned 100 total points
ID: 34886562
Try this code. You can write your own function having the string and the tag to remove as input

 
string strA = "<p style='margin-top: 0pt; margin-right: 0pt; margin-bottom: 0pt; margin-left: 0pt'>kajf aksfja skfjas fkajsf asjf sakfjas fkasjf kasfj safjsinancial video content has posted the latest <a href='http://www.testh.com/media/763/Dynacor_Gold_Mines_TSX:_DNG_News_Alert/' target='_blank'>video</a> 'News Alert' for Dynacor Gold Mines Inc. If the link is unavailable, please visit <a href='http://www.kfjaskfjsaf.com/' target='_blank'>www.Isfsf.com</a> and enter 'Dynacor' in the search box.</p>";
            int posStart;
            int posEnd;
            string tagToRemove="<p";
            string tagToReplace = "</p>";

            do
            {
                posStart = strA.IndexOf(tagToRemove);
                if (posStart>=0 )
                {
                    posEnd = strA.IndexOf(">", posStart+1);
                    strA = strA.Substring(0, posStart) + strA.Substring(posEnd + 1);

                }
            }
            while (posStart >= 0);

            strA = strA.Replace(tagToReplace, "</BR></BR>");
            MessageBox.Show(strA);

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 34889178
How about this?
string input = "<p style=\"margin-top: 0pt; margin-right: 0pt; margin-bottom: 0pt; margin-left: 0pt\">kajf aksfja skfjas fkajsf asjf sakfjas fkasjf kasfj safjsinancial video content has posted the latest <a href=\"http://www.testh.com/media/763/Dynacor_Gold_Mines_TSX:_DNG_News_Alert/\" target=\"_blank\">video</a> \"News Alert\" for Dynacor Gold Mines Inc. If the link is unavailable, please visit <a href=\"http://www.kfjaskfjsaf.com/\" target=\"_blank\">www.Isfsf.com</a> and enter \"Dynacor\" in the search box.</p>";
string result;

result = System.Text.RegularExpressions.Regex.Replace(input, "<p [^>]+", string.Empty, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
result = System.Text.RegularExpressions.Regex.Replace(result, "</p>", "<BR /><BR />", System.Text.RegularExpressions.RegexOptions.IgnoreCase);

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 34889188
Correction to line 4:
result = System.Text.RegularExpressions.Regex.Replace(input, "<p [^>]+>", string.Empty, System.Text.RegularExpressions.RegexOptions.IgnoreCase);

Open in new window

0
 
LVL 2

Author Comment

by:saloj
ID: 34895962
Hi Kaufmed,
how about for both <p> with attributes and only <p>
string input = "<p style=\"margin-top: 0pt; margin-right: 0pt; margin-bottom: 0pt; margin-left: 0pt\">kajf aksfja"
string input = "<p>kajf aksfja"
0
 
LVL 7

Expert Comment

by:kemi67
ID: 34896604
Try this
string input = "<p>kajf aksfja"
string result;
result = System.Text.RegularExpressions.Regex.Replace(input, "<p([^>]+>|>)", string.Empty, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
result = System.Text.RegularExpressions.Regex.Replace(result, "</p>", "<BR /><BR />", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
MessageBox.Show(result);

Open in new window

0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 400 total points
ID: 34896675
kemi67's example is flawed, and its intent (if it were corrected) would also remove <pre> tags, which you may or may not want. To hone in on only the <p> tags, use:
result = System.Text.RegularExpressions.Regex.Replace(input, "<p(?: [^>]+)?>", string.Empty, System.Text.RegularExpressions.RegexOptions.IgnoreCase);

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 34896684
Actually, disregard the "flawed" comment....  I missed the parentheses. The "<pre>" comment still applies though  : )
0
 
LVL 7

Expert Comment

by:kemi67
ID: 34897202
kaufmed is right, his solution works for removing only <p>.
Is is a brilliant solution
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
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 …

710 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