Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HI
what I m trying to do is to only replace multiple <br> tags occurring simultaneously e.g. ....<br><br><br><br>... should be replaced by single <br> tag.
result = Regex.Replace(result, "(\r)( )+(\r)", "\r\r", RegexOptions.IgnoreCase);

I have tried
C#
{<br>}( )+{<br>}
[<br>]( )+[<br>]
(<br>)( )+(<br>)

But it doesn't work.

Thanks,
A
Posted
Updated 19-Mar-15 23:42pm
v4
Comments
OriginalGriff 20-Mar-15 4:37am    
What are you actually trying to do?
Even with your values encoded so they don;t get swallowed, it's not obvious what you arr trying to achieve.
Perhaps an example of your input, and desired output would help us to understand?

Use the "Improve question" widget to edit your question and provide better information.
ASJ_SA 20-Mar-15 4:45am    
I have an email coming in that needs to be formatted to remove extra spaces etc.
So I go through the html of the email and replace the multiple breaks.
so the code m using to do this is:
result = Regex.Replace(result, "(\r)( )+(\r)", "\r\r", RegexOptions.IgnoreCase);
but I need to also remove the multiple <br> tags

Thanks7872 20-Mar-15 5:21am    
Use reply button on comment. Otherwise the person won't come to know about your response.

If you want to replace all the
<br>
with "", try Regex.Replace() method:
XML
Imports System
Imports System.Text.RegularExpressions

Public Module Module1
    Public Sub Main()
        ' original string
        Dim input As String = "blah<br> blah<br>,,,"
        Dim output As String = Regex.Replace(input, "<br>", "")
        ' print out
        Console.WriteLine(input)
        Console.WriteLine(output)
    End Sub
End Module

++++[Round 2]+++++
If you meant to replace consecutive breaks like
blah<br><br>blah

with a single
<br>

then try this:
XML
Dim input As String = "blah<br> blah<br><br>,,,"
Dim output As String = Regex.Replace(input, "(<br>){2,}", "<br>")

{2,} means 2 or more of the preceding pattern.
 
Share this answer
 
v4
Comments
ASJ_SA 20-Mar-15 4:48am    
hi Peter, Thanks
but what I m trying to do is to oonly replace multiple <br> tags occurring simultaneously
result = Regex.Replace(result, "(\r)( )+(\r)", "\r\r", RegexOptions.IgnoreCase);
I need something like this but with <br> tags
Peter Leow 20-Mar-15 5:00am    
Not clear. Can you show an example of "before" vs "after"?
ASJ_SA 20-Mar-15 5:06am    
result = Regex.Replace(result, "(\r)( )+(\r)", "\r\r", RegexOptions.IgnoreCase);
this line of code replaces multiple occurance of \r with "\r\r"
What I want is to look for multiple occurance of <br>(breaks in html) and replace with possibly one <br>
so I tried
result = Regex.Replace(result, "(<br>)( )+(<br>)", "<br>", RegexOptions.IgnoreCase);
but it doesn even find the multiple <br> tags
I think its got something to do with the "<" in the string
bcoz msdn says its to indicate beginning of word.
Beginning of word < Matches only when a word begins at this point in the text.
so now I need to find out how to escape it..
I did try putting \ but no use
Thanks7872 20-Mar-15 5:23am    
You want ....<br><br><br><br><br>... should be replaced by single <br>,but single one should not be replaced?
ASJ_SA 20-Mar-15 5:39am    
yes:)
Try this :
C#
Dim Your_String As String = "dsfsdfsdfsdfs <br> dsfsdfsdfsdfsdf <br><br><br><br><br><br> sdfsdf <br><br> sdfgssdg <br><br><br> sdfdsf <br> dssfsf <br>"

Dim options As RegexOptions = RegexOptions.None

Dim regex As New Regex("[<br>]{2,}", options)

Your_String = regex.Replace(Your_String, "<br>")

Reference : http://stackoverflow.com/a/206720/2645738[^]

Regards..
 
Share this answer
 
v2
Comments
ASJ_SA 20-Mar-15 9:53am    
Thnks Rohan,...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900