Solved

string search and replace

Posted on 2011-02-13
11
275 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
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
 
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

920 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

16 Experts available now in Live!

Get 1:1 Help Now