Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

string search and replace

Posted on 2011-02-13
11
Medium Priority
?
285 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
Quick Start: DOCKER

Sometimes you just need a Quick Start on a topic in order to begin using it.. this is just what you need to know to get up and running with Docker!

 
LVL 7

Assisted Solution

by:kemi67
kemi67 earned 400 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 1600 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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

715 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