Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

string search and replace

Posted on 2011-02-13
11
Medium Priority
?
289 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: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
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Screencast - Getting to Know the Pipeline

972 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