Solved

string search and replace

Posted on 2011-02-13
11
274 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
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 12

Expert Comment

by:Mamtha1982
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 7

Assisted Solution

by:kemi67
kemi67 earned 100 total points
Comment Utility
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 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
Comment Utility
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
Comment Utility
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 74

Accepted Solution

by:
käµfm³d   👽 earned 400 total points
Comment Utility
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 74

Expert Comment

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

Expert Comment

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

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

771 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

13 Experts available now in Live!

Get 1:1 Help Now