Showing posts with label tags. Show all posts
Showing posts with label tags. Show all posts

Monday, March 26, 2012

Stringbuilder Parsing Text: Need to remove square character

Hi everyone,

I am attempting to parse data from a webpage and want to remove any unneccesary junk from the string. I start by removing any Tags from the text I have retrieved from the web page. Next, I split the text into an array. However, I end up with a string that contains a bunch of characters that look like ?, sometimes there are many in a row: ???? in each string. I would like to remove these characters.

These characters are the only things left separating me from the data I want, but I am not sure how to remove them... Does anyone have an idea? It is interesting that when I display my string in a textbox, or using a label, the squares are not visible (I think they display as spaces) but when I debug I see them.

Thanks,

zoop

Those chars probably are line feeds, tabs or some other control chars.

Try:

Dim junk asString ="bla bla bla....."

junk=junk.replace(ControlChars.Lf,"")

junk=junk.replace(ControlChars.NewLine,"")

junk=junk.replace(ControlChars.Tab,"")


etc, etc...


If I'm not mistaken you are seeing the hidden space place holder. You can use regular expressions to remove them. Try something like below to remove them.

using System.Text.RegularExpressions;

private string StripSpaces(string inputString)
{
return Regex.Replace(inputString, @."[^\s$]","");
}


Thanks for the input fellas. I have tried the suggestions above along with some variations and came up short. Replacing the ControlChars does not seem to make a difference on the format of the string. Running the stripSpaces function removes all of the data I am interested in and returns only the chars I am attempting to remove (blanks, or squares). Perhaps I lost something when I translated it into VB...

Anyway, since there is not much code to go over I thought perhaps it would help if I posted what I have here. I am going to omit the section where I replace any ControlChars for brevity, as it does not seem to make a difference.

1Private Sub btnSubmit_Click(ByVal senderAs Object,ByVal eAs System.EventArgs)Handles btnSubmit.Click2'// Store Page Source in DOC3Dim reqAs WebRequest = WebRequest.Create("http://world5.knightfight.co.uk/index.php?ac=highscore&vid=0")4Dim respAs WebResponse = req.GetResponse56Dim sAs Stream = resp.GetResponseStream7Dim srAs StreamReader =New StreamReader(s, Encoding.ASCII)8Dim docAs String = sr.ReadToEnd910'// Format text in DOC using StringBuilder Class11Dim sbAs StringBuilder =New StringBuilder(doc)12Dim endIndexAs Integer = sb.ToString.IndexOf("showuserid=")13Dim newString()As String14Dim strTestAs String1516'// Remove Unwanted Text17strTest = sb.Remove(0, endIndex).ToString1819'// Remove HTML Tags and Spaces20strTest = StripTags(strTest)21strTest = stripSpaces(strTest)2223'// TEMP - Display Preview in Multiline Textbox24txtbWebData.Text = strTest2526'// Split String into Array27newString = Split(strTest," ")28End Sub2930Public Function StripTags(ByVal HTMLAs String)As String31' Removes tags from passed HTML32Dim objRegExAs _33System.Text.RegularExpressions.Regex34Return objRegEx.Replace(HTML,"<[^>]*>","*")35End Function3637Public Function stripSpaces(ByVal inputStringAs String)As String38Dim objRegExAs _39System.Text.RegularExpressions.Regex40Return objRegEx.Replace(inputString,"[^\s$]","")41End Function42

I thought it would be an interesting exercise to try and capture some data from a highscores list on a website. The URL is in the code above, http://world5.knightfight.co.uk/index.php?ac=highscore&vid=0,so you could reproduce my results if you like. The end goal is to capture the data in the highscores list and format it in a way that makes sense. If you think there is a better approach please let me know :).

Thanks,

zoop


Sorry about that. Replace Return objRegEx.Replace(inputString, "[^\s$]", "") with Return objRegEx.Replace(inputString, "[^\S$]", ""). The \s matches any white space character and the \S matches any non-white space character.

strings

i display tags on the page separated by commas

eg:tag1,tag2,tag3

if i have only 1 tag it should add

eg:tag1

not

eg:tag1,

if i have only 2 tag it should add

eg:tag1,tag2

not

eg:tag1,tag2,

how to do that because my functions returns with comma

while

(dtardrTagOther.Read())

{

strOtherTags += dtardrTagOther.GetValue(0).ToString()+ ",";

}

Hello,Just discard the last comma from the strOtherTags after the end of while loop. like

while

(dtardrTagOther.Read())

{

strOtherTags += dtardrTagOther.GetValue(0).ToString()+ ",";

}

strOtherTags = strOtherTags.Remove(strOtherTags.Length-1,1);

!!! Mark as Answer if it meets your requirement !!!


Thanks.

Try the below code

while(dtardrTagOther.Read())

{

if (strOtherTags == string.Empty)

{

strOtherTags = dtardrTagOther.GetValue(0).ToString();

}

else

strOtherTags += "," + dtardrTagOther.GetValue(0).ToString();

}

HC


after u exit while:

strOtherTags = strOtherTags.Substring(0, strOtherTags.Length - 1)

HTH

Saturday, March 24, 2012

Strip HTML from Text

Is there an easy way to strip HTML tags from Text to get just the plain
text?
I am using a program called FreeTextBox that lets you format Text in a
TextBox. It does this by adding HTML tags (<b>, <u>,<span> etc) to the
code.
The problem is that it is a problem since I am putting the text in a
varChar(8000). The HTML adds a lot characters to the text. I can change
this to a Text field in Sql, but you can do a FullText search on a Text
field (plus I don't really want the tags in a Text Search).
Is there some filtering mechanism that would strip the HTML from a text
field?
Thanks,
TomHere's a complete sample :
http://www.experience247.com/srcvie...tmlIn.cs&font=3
Juan T. Llibre, asp.net MVP
aspnetfaq.com : http://www.aspnetfaq.com/
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en espaol : http://asp.net.do/foros/
===================================
"tshad" <tscheiderich@.ftsolutions.com> wrote in message
news:%23UjtGFlPGHA.1580@.TK2MSFTNGP09.phx.gbl...
> Is there an easy way to strip HTML tags from Text to get just the plain te
xt?
> I am using a program called FreeTextBox that lets you format Text in a Tex
tBox. It does this by
> adding HTML tags (<b>, <u>,<span> etc) to the code.
> The problem is that it is a problem since I am putting the text in a varCh
ar(8000). The HTML adds
> a lot characters to the text. I can change this to a Text field in Sql, b
ut you can do a FullText
> search on a Text field (plus I don't really want the tags in a Text Search
).
> Is there some filtering mechanism that would strip the HTML from a text fi
eld?
> Thanks,
> Tom
>
That does what I was looking for.
Thanks,
Tom
"Juan T. Llibre" <nomailreplies@.nowhere.com> wrote in message
news:OMXrAJlPGHA.4952@.TK2MSFTNGP09.phx.gbl...
> Here's a complete sample :
> http://www.experience247.com/srcvie...tmlIn.cs&font=3
>
> Juan T. Llibre, asp.net MVP
> aspnetfaq.com : http://www.aspnetfaq.com/
> asp.net faq : http://asp.net.do/faq/
> foros de asp.net, en espaol : http://asp.net.do/foros/
> ===================================
> "tshad" <tscheiderich@.ftsolutions.com> wrote in message
> news:%23UjtGFlPGHA.1580@.TK2MSFTNGP09.phx.gbl...
>

Strip HTML from Text

Is there an easy way to strip HTML tags from Text to get just the plain
text?

I am using a program called FreeTextBox that lets you format Text in a
TextBox. It does this by adding HTML tags (<b>, <u>,<span> etc) to the
code.

The problem is that it is a problem since I am putting the text in a
varChar(8000). The HTML adds a lot characters to the text. I can change
this to a Text field in Sql, but you can do a FullText search on a Text
field (plus I don't really want the tags in a Text Search).

Is there some filtering mechanism that would strip the HTML from a text
field?

Thanks,

TomHere's a complete sample :

http://www.experience247.com/srcvie...tmlIn.cs&font=3

Juan T. Llibre, asp.net MVP
aspnetfaq.com : http://www.aspnetfaq.com/
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en espaol : http://asp.net.do/foros/
===================================
"tshad" <tscheiderich@.ftsolutions.com> wrote in message
news:%23UjtGFlPGHA.1580@.TK2MSFTNGP09.phx.gbl...
> Is there an easy way to strip HTML tags from Text to get just the plain text?
> I am using a program called FreeTextBox that lets you format Text in a TextBox. It does this by
> adding HTML tags (<b>, <u>,<span> etc) to the code.
> The problem is that it is a problem since I am putting the text in a varChar(8000). The HTML adds
> a lot characters to the text. I can change this to a Text field in Sql, but you can do a FullText
> search on a Text field (plus I don't really want the tags in a Text Search).
> Is there some filtering mechanism that would strip the HTML from a text field?
> Thanks,
> Tom
That does what I was looking for.

Thanks,

Tom
"Juan T. Llibre" <nomailreplies@.nowhere.com> wrote in message
news:OMXrAJlPGHA.4952@.TK2MSFTNGP09.phx.gbl...
> Here's a complete sample :
> http://www.experience247.com/srcvie...tmlIn.cs&font=3
>
> Juan T. Llibre, asp.net MVP
> aspnetfaq.com : http://www.aspnetfaq.com/
> asp.net faq : http://asp.net.do/faq/
> foros de asp.net, en espaol : http://asp.net.do/foros/
> ===================================
> "tshad" <tscheiderich@.ftsolutions.com> wrote in message
> news:%23UjtGFlPGHA.1580@.TK2MSFTNGP09.phx.gbl...
>> Is there an easy way to strip HTML tags from Text to get just the plain
>> text?
>>
>> I am using a program called FreeTextBox that lets you format Text in a
>> TextBox. It does this by adding HTML tags (<b>, <u>,<span> etc) to the
>> code.
>>
>> The problem is that it is a problem since I am putting the text in a
>> varChar(8000). The HTML adds a lot characters to the text. I can change
>> this to a Text field in Sql, but you can do a FullText search on a Text
>> field (plus I don't really want the tags in a Text Search).
>>
>> Is there some filtering mechanism that would strip the HTML from a text
>> field?
>>
>> Thanks,
>>
>> Tom
>>

Strip html tags from listbox

I've been looking around with no luck for the way to get rid of the html tags from showing in a listbox being popullated from the db.

Where have I not looked for this solution?

Thanks all,

ZathYou could always htmlEncode the contents of the listbox. That wouldn't strip away the tags, just convert them. For example <tag> would become <tag>.

You could always write a function that searches for the "<" and ">" characters and strips them off.

Jeff
Yea, thought of that.

It's just in PHP, it's something easy like strip_html or something like that...
Thought and searched for something similar in .Net

Zath

Strip Visual Studio META Tags Is Safe?

I have these in all of my pages:

<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name=vs_defaultClientScript content="JavaScript">
<meta name=vs_targetSchema
content="http://schemas.microsoft.com/intellisense/ie5"
And I am wondering if it is safe to remove them after I compile the
ASP.NET application, or if it is possible to have VS.NET 2003 not put
them in my .aspx pages in the first place.

Thanks.meta tags are not rendered, they are only used to provide information to the
server and the client. They can be deleted at will, if you don't need them.
Removing the target schema and client script meta tags will remove some
intellisense features on the development environment, but nothing contained
in a meta tag will affect the rendering of the page itself.

--
Chris Jackson
Software Engineer
Microsoft MVP - Windows Client
Windows XP Associate Expert
--
More people read the newsgroups than read my email.
Reply to the newsgroup for a faster response.
(Control-G using Outlook Express)
--

"localhost" <primpilus@.cohort.ces> wrote in message
news:3ubf20p4i2t4k30u4je2hjmv5atl7g4e4v@.4ax.com...
>I have these in all of my pages:
> <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
> <meta name="CODE_LANGUAGE" Content="C#">
> <meta name=vs_defaultClientScript content="JavaScript">
> <meta name=vs_targetSchema
> content="http://schemas.microsoft.com/intellisense/ie5">
> And I am wondering if it is safe to remove them after I compile the
> ASP.NET application, or if it is possible to have VS.NET 2003 not put
> them in my .aspx pages in the first place.
> Thanks.
Hi Localhost,

Thanks for posting in the community!
From your description, you are wondering some infos on the <meta> tags
VS.NET has automatically put in the ASP.NET page when page is created and
whether it'll cause any problem if remove them, yes?
If there is anything I misunderstood, please feel free to let me know.

As for this problem, I agree to Chris's opinion, the meta tags commonly
tells web browsers basic host information about a site or the tools that
created the source files of the site. This information is also used by
search engines. For detailed info on the META tags, you may view the
following web references:

#META - Metadata
http://www.htmlhelp.com/reference/html40/head/meta.html

#HTML Tutorial -> META - Metadata
http://www.style-sheets.com/html_tutorial/head/meta.asp

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Stripping html tags from text

Hi,
I'm looking for help with a regular expression and c#.
I want to remove all tags from a piece of html except the following.
<a>
<b>
<h1>
<h2>
<h3>
Also, <a> could be <a href="http://links.10026.com/?link=aa">aaa</a> etc.
Help would be appreciated, along with an explanation of the reg
expression created.
Thanks.HTML is complex. It would be better instead to say that you want to
*retrieve* *only* all of the following tags. That way, they are the only
tags the Regular Expression will have to look for.
The following will do this:
(?i)<\s*(a|br|h1|h2|h3)[^>]*>(?:([^<\r\n]+)(?=(?:<\/\1)|(?:\r?\n)))?
Note: Grouping is used in this Regular Expression. It groups the tag names
into Group 1, and the InnerText into Group 2, in case you need either of
these.
HTH,
Kevin Spencer
Microsoft MVP
.Net Developer
Presuming that God is "only an idea" -
Ideas exist.
Therefore, God exists.
"Spondishy" <spondishy@.tiscali.co.uk> wrote in message
news:1141639561.492632.61150@.z34g2000cwc.googlegroups.com...
> Hi,
> I'm looking for help with a regular expression and c#.
> I want to remove all tags from a piece of html except the following.
> <a>
> <b>
> <h1>
> <h2>
> <h3>
> Also, <a> could be <a href="http://links.10026.com/?link=aa">aaa</a> etc.
> Help would be appreciated, along with an explanation of the reg
> expression created.
> Thanks.
>

i use this in VB
Private Function stripHTML(ByVal strHTML) As String
Dim objRegExp As New System.Text.RegularExpressions.Regex("<(.|\n)+?>")
Return objRegExp.Replace(strHTML, "")
End Function
so the regex System.Text.RegularExpressions.Regex("<(.|\n)+?>")
does the trick
so in C# it would be ( i am a VB coder so don`t shoot me )
private string stripHTML(object strHTML)
{
System.Text.RegularExpressions.Regex objRegExp = new
System.Text.RegularExpressions.Regex("<(.|\n)+?>");
return objRegExp.Replace(strHTML, "");
}
regards
Michel Posseth [MCP]
"Spondishy" <spondishy@.tiscali.co.uk> wrote in message
news:1141639561.492632.61150@.z34g2000cwc.googlegroups.com...
> Hi,
> I'm looking for help with a regular expression and c#.
> I want to remove all tags from a piece of html except the following.
> <a>
> <b>
> <h1>
> <h2>
> <h3>
> Also, <a> could be <a href="http://links.10026.com/?link=aa">aaa</a> etc.
> Help would be appreciated, along with an explanation of the reg
> expression created.
> Thanks.
>
The problem with that Regular Expression (in this case) is that it simply
matches all tags in the page. It doesn't match InnerText, as he requested,
and it matches end tags as separate matches. It is excellent for, for
example, stripping HTML tags from a page, but not for his requirements.
HTH,
Kevin Spencer
Microsoft MVP
.Net Developer
Presuming that God is "only an idea" -
Ideas exist.
Therefore, God exists.
"m.posseth" <michelp@.nohausystems.nl> wrote in message
news:%23kpfPDSQGHA.5092@.TK2MSFTNGP11.phx.gbl...
>
> i use this in VB
> Private Function stripHTML(ByVal strHTML) As String
> Dim objRegExp As New System.Text.RegularExpressions.Regex("<(.|\n)+?>")
> Return objRegExp.Replace(strHTML, "")
> End Function
> so the regex System.Text.RegularExpressions.Regex("<(.|\n)+?>")
> does the trick
> so in C# it would be ( i am a VB coder so don`t shoot me )
> private string stripHTML(object strHTML)
> {
> System.Text.RegularExpressions.Regex objRegExp = new
> System.Text.RegularExpressions.Regex("<(.|\n)+?>");
> return objRegExp.Replace(strHTML, "");
> }
> regards
> Michel Posseth [MCP]
>
>
> "Spondishy" <spondishy@.tiscali.co.uk> wrote in message
> news:1141639561.492632.61150@.z34g2000cwc.googlegroups.com...
>
Oops :-)
i just read "Stripping html tags from text" and missed the exclusion part

my code will convert
<html>
<head>
<body>
<table>
<tr><td>bla bla </td></tr>
</table>
</body>
</head>
</html>
into
bla bla
regards
Michel
"Kevin Spencer" <kevin@.DIESPAMMERSDIEtakempis.com> wrote in message
news:eISasoSQGHA.4900@.TK2MSFTNGP09.phx.gbl...
> The problem with that Regular Expression (in this case) is that it simply
> matches all tags in the page. It doesn't match InnerText, as he requested,
> and it matches end tags as separate matches. It is excellent for, for
> example, stripping HTML tags from a page, but not for his requirements.
> --
> HTH,
> Kevin Spencer
> Microsoft MVP
> .Net Developer
> Presuming that God is "only an idea" -
> Ideas exist.
> Therefore, God exists.
> "m.posseth" <michelp@.nohausystems.nl> wrote in message
> news:%23kpfPDSQGHA.5092@.TK2MSFTNGP11.phx.gbl...
>

Stripping html tags from text

Hi,

I'm looking for help with a regular expression and c#.

I want to remove all tags from a piece of html except the following.

<a>
<b>
<h1>
<h2>
<h3
Also, <a> could be <a href="http://links.10026.com/?link=aa">aaa</a> etc.

Help would be appreciated, along with an explanation of the reg
expression created.

Thanks.HTML is complex. It would be better instead to say that you want to
*retrieve* *only* all of the following tags. That way, they are the only
tags the Regular Expression will have to look for.

The following will do this:

(?i)<\s*(a|br|h1|h2|h3)[^>]*>(?:([^<\r\n]+)(?=(?:<\/\1)|(?:\r?\n)))?

Note: Grouping is used in this Regular Expression. It groups the tag names
into Group 1, and the InnerText into Group 2, in case you need either of
these.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer

Presuming that God is "only an idea" -
Ideas exist.
Therefore, God exists.

"Spondishy" <spondishy@.tiscali.co.uk> wrote in message
news:1141639561.492632.61150@.z34g2000cwc.googlegro ups.com...
> Hi,
> I'm looking for help with a regular expression and c#.
> I want to remove all tags from a piece of html except the following.
> <a>
> <b>
> <h1>
> <h2>
> <h3>
> Also, <a> could be <a href="http://links.10026.com/?link=aa">aaa</a> etc.
> Help would be appreciated, along with an explanation of the reg
> expression created.
> Thanks.

i use this in VB

Private Function stripHTML(ByVal strHTML) As String

Dim objRegExp As New System.Text.RegularExpressions.Regex("<(.|\n)+?>")

Return objRegExp.Replace(strHTML, "")

End Function

so the regex System.Text.RegularExpressions.Regex("<(.|\n)+?>")

does the trick

so in C# it would be ( i am a VB coder so don`t shoot me )

private string stripHTML(object strHTML)

{

System.Text.RegularExpressions.Regex objRegExp = new
System.Text.RegularExpressions.Regex("<(.|\n)+?>");

return objRegExp.Replace(strHTML, "");

}

regards

Michel Posseth [MCP]

"Spondishy" <spondishy@.tiscali.co.uk> wrote in message
news:1141639561.492632.61150@.z34g2000cwc.googlegro ups.com...
> Hi,
> I'm looking for help with a regular expression and c#.
> I want to remove all tags from a piece of html except the following.
> <a>
> <b>
> <h1>
> <h2>
> <h3>
> Also, <a> could be <a href="http://links.10026.com/?link=aa">aaa</a> etc.
> Help would be appreciated, along with an explanation of the reg
> expression created.
> Thanks.
The problem with that Regular Expression (in this case) is that it simply
matches all tags in the page. It doesn't match InnerText, as he requested,
and it matches end tags as separate matches. It is excellent for, for
example, stripping HTML tags from a page, but not for his requirements.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer

Presuming that God is "only an idea" -
Ideas exist.
Therefore, God exists.

"m.posseth" <michelp@.nohausystems.nl> wrote in message
news:%23kpfPDSQGHA.5092@.TK2MSFTNGP11.phx.gbl...
>
> i use this in VB
> Private Function stripHTML(ByVal strHTML) As String
> Dim objRegExp As New System.Text.RegularExpressions.Regex("<(.|\n)+?>")
> Return objRegExp.Replace(strHTML, "")
> End Function
> so the regex System.Text.RegularExpressions.Regex("<(.|\n)+?>")
> does the trick
> so in C# it would be ( i am a VB coder so don`t shoot me )
> private string stripHTML(object strHTML)
> {
> System.Text.RegularExpressions.Regex objRegExp = new
> System.Text.RegularExpressions.Regex("<(.|\n)+?>");
> return objRegExp.Replace(strHTML, "");
> }
> regards
> Michel Posseth [MCP]
>
>
> "Spondishy" <spondishy@.tiscali.co.uk> wrote in message
> news:1141639561.492632.61150@.z34g2000cwc.googlegro ups.com...
>> Hi,
>>
>> I'm looking for help with a regular expression and c#.
>>
>> I want to remove all tags from a piece of html except the following.
>>
>> <a>
>> <b>
>> <h1>
>> <h2>
>> <h3>
>>
>> Also, <a> could be <a href="http://links.10026.com/?link=aa">aaa</a> etc.
>>
>> Help would be appreciated, along with an explanation of the reg
>> expression created.
>>
>> Thanks.
>>
Oops :-)

i just read "Stripping html tags from text" and missed the exclusion part

>>>except the following.
>>>
>>> <a>
>>> <b>
>>> <h1>
>>> <h2>
>>> <h3>
>>>
>>> Also, <a> could be <a href="http://links.10026.com/?link=aa">aaa</a> etc.

my code will convert
<html>
<head>
<body>
<table>
<tr><td>bla bla </td></tr>
</table>
</body>
</head>
</html
into

bla bla

regards

Michel

"Kevin Spencer" <kevin@.DIESPAMMERSDIEtakempis.com> wrote in message
news:eISasoSQGHA.4900@.TK2MSFTNGP09.phx.gbl...
> The problem with that Regular Expression (in this case) is that it simply
> matches all tags in the page. It doesn't match InnerText, as he requested,
> and it matches end tags as separate matches. It is excellent for, for
> example, stripping HTML tags from a page, but not for his requirements.
> --
> HTH,
> Kevin Spencer
> Microsoft MVP
> .Net Developer
> Presuming that God is "only an idea" -
> Ideas exist.
> Therefore, God exists.
> "m.posseth" <michelp@.nohausystems.nl> wrote in message
> news:%23kpfPDSQGHA.5092@.TK2MSFTNGP11.phx.gbl...
>>
>>
>> i use this in VB
>>
>> Private Function stripHTML(ByVal strHTML) As String
>>
>> Dim objRegExp As New System.Text.RegularExpressions.Regex("<(.|\n)+?>")
>>
>> Return objRegExp.Replace(strHTML, "")
>>
>> End Function
>>
>> so the regex System.Text.RegularExpressions.Regex("<(.|\n)+?>")
>>
>> does the trick
>>
>> so in C# it would be ( i am a VB coder so don`t shoot me )
>>
>> private string stripHTML(object strHTML)
>>
>> {
>>
>> System.Text.RegularExpressions.Regex objRegExp = new
>> System.Text.RegularExpressions.Regex("<(.|\n)+?>");
>>
>> return objRegExp.Replace(strHTML, "");
>>
>> }
>>
>> regards
>>
>> Michel Posseth [MCP]
>>
>>
>>
>>
>>
>> "Spondishy" <spondishy@.tiscali.co.uk> wrote in message
>> news:1141639561.492632.61150@.z34g2000cwc.googlegro ups.com...
>>> Hi,
>>>
>>> I'm looking for help with a regular expression and c#.
>>>
>>> I want to remove all tags from a piece of html except the following.
>>>
>>> <a>
>>> <b>
>>> <h1>
>>> <h2>
>>> <h3>
>>>
>>> Also, <a> could be <a href="http://links.10026.com/?link=aa">aaa</a> etc.
>>>
>>> Help would be appreciated, along with an explanation of the reg
>>> expression created.
>>>
>>> Thanks.
>>>
>>
>>

stripping out html tags for plain txt email

I was wondering what the best way to strip the html tags off of a page and add vbclf and such for plain text emailing of web pages

if you could point me in the right direction it would be greatTo remove HTML tags from awell-formed document:
Converting HTML to Text
awesome what a life saver
now how could I change the code below so that it replaces the </p> and <br> with a _vbclrf or a plaintext line break and also how can I get it to remove all of the dead space I see a comment posted on the page you linked about doing it but no one actually does


Public Function StripTags(ByVal HTML As String) As String
' Removes tags from passed HTML
Dim objRegEx As _
System.Text.RegularExpressions.Regex
Return objRegEx.Replace(HTML, "<[^>]*>", "")
End Function

Try the following:
 Import System.Text.RegularExpressions

Public Function StripTags(ByVal HTML As String) As String
Dim cleanString As String = HTML
Dim objRegEx As Regex

' First, remove whitespace
objRegEx = New RegEx( "\s{2,}" )
cleanString = objRegEx.Replace( cleanString, " " )

' Second, replace HTML linebreaks with text line breaks
objRegEx = New RegEx( "((</p>)|(<br ?/?>))" )
cleanString = objRegEx.Replace( cleanString, System.Environment.NewLine )

' Third, clean up any occurrence of newline + space
objRegEx = New RegEx( "(^|\n) +" )
cleanString = objRegEx.Replace( cleanString, String.Empty)

' Finally, remove HTML tags
objRegEx = New RegEx( "<[^>]*?>" )
cleanString = objRegEx.Replace( cleanString, String.Empty)

Return cleanString
End Function

stripping tags from source on render

Hi,
What way could I strip certain tags (like HTML comments) from the HTML being
delivered to the client? I don't mean what regexp to use, but where do I
put this stripping code? I'm thinking something in the Global.asax, but I
can't find any reference to it having a Render or PreRender event or how to
tie into them if I did! I'd ideally like some help along the lines of:

1 - Where to put the code
2 - example of how to alter the source being delivered to the client

Thanks,
LanceHi Lance:

You could use a Response.Filter, like the one in this article:
http://www.codeproject.com/aspnet/R...pacesAspNet.asp

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Fri, 17 Jun 2005 11:43:50 +0100, "Lance"
<lance@.[nospam]keayweb.com> wrote:

>Hi,
>What way could I strip certain tags (like HTML comments) from the HTML being
>delivered to the client? I don't mean what regexp to use, but where do I
>put this stripping code? I'm thinking something in the Global.asax, but I
>can't find any reference to it having a Render or PreRender event or how to
>tie into them if I did! I'd ideally like some help along the lines of:
>1 - Where to put the code
>2 - example of how to alter the source being delivered to the client
>Thanks,
>Lance
Hi Lance,

I'd look at the Page.Render event. This takes an HtmlTextWriter parameter
where you can modify the contents before calling the base.Render method.

HTH
--
Ian Lane

"Lance" wrote:

> Hi,
> What way could I strip certain tags (like HTML comments) from the HTML being
> delivered to the client? I don't mean what regexp to use, but where do I
> put this stripping code? I'm thinking something in the Global.asax, but I
> can't find any reference to it having a Render or PreRender event or how to
> tie into them if I did! I'd ideally like some help along the lines of:
> 1 - Where to put the code
> 2 - example of how to alter the source being delivered to the client
> Thanks,
> Lance
>
Thanks! I thought i had seen it somewhere on good 'ol codeproject!

"Scott Allen" <scott@.nospam.odetocode.com> wrote in message
news:k3s5b1drg28qbudj23ggjs1l8r861cuqhh@.4ax.com...
> Hi Lance:
> You could use a Response.Filter, like the one in this article:
> http://www.codeproject.com/aspnet/R...pacesAspNet.asp
> --
> Scott
> http://www.OdeToCode.com/blogs/scott/
> On Fri, 17 Jun 2005 11:43:50 +0100, "Lance"
> <lance@.[nospam]keayweb.com> wrote:
> >Hi,
> >What way could I strip certain tags (like HTML comments) from the HTML
being
> >delivered to the client? I don't mean what regexp to use, but where do I
> >put this stripping code? I'm thinking something in the Global.asax, but
I
> >can't find any reference to it having a Render or PreRender event or how
to
> >tie into them if I did! I'd ideally like some help along the lines of:
> >1 - Where to put the code
> >2 - example of how to alter the source being delivered to the client
> >Thanks,
> >Lance
I'd have to call the code from every page using this method, right? If I
could call it from one location (global.asax) that would be great!

"Ian Lane .enizin.net>" <ian@.<nospam> wrote in message
news:329AC863-65C0-41DC-B99E-2C47F83EA16F@.microsoft.com...
> Hi Lance,
> I'd look at the Page.Render event. This takes an HtmlTextWriter parameter
> where you can modify the contents before calling the base.Render method.
> HTH
> --
> Ian Lane
>
> "Lance" wrote:
> > Hi,
> > What way could I strip certain tags (like HTML comments) from the HTML
being
> > delivered to the client? I don't mean what regexp to use, but where do
I
> > put this stripping code? I'm thinking something in the Global.asax, but
I
> > can't find any reference to it having a Render or PreRender event or how
to
> > tie into them if I did! I'd ideally like some help along the lines of:
> > 1 - Where to put the code
> > 2 - example of how to alter the source being delivered to the client
> > Thanks,
> > Lance

Tuesday, March 13, 2012

stuck on a REGEX (\S[^\s/>]*)

I'm trying to find the opening < and the text of a tag (without the
attributes or closing tags)
This is what I'm using:
(\S[^\s/>]*)
Which, I think, reads as:
(any number of non-whitespace characters [up to a space, /, or >])
Is that correct? I can't get it to work.
If my text is:
<tag
then it returns "<tag" which is what I want.
However, if I have:
<tag/ or <tag>
it instead matches "/" or ">" respectively.
Why?darrel wrote:
> I'm trying to find the opening < and the text of a tag (without the
> attributes or closing tags)
> This is what I'm using:
> (\S[^\s/>]*)
> Which, I think, reads as:
> (any number of non-whitespace characters [up to a space, /, or >])
> Is that correct? I can't get it to work.
> If my text is:
> <tag
> then it returns "<tag" which is what I want.
> However, if I have:
> <tag/ or <tag>
> it instead matches "/" or ">" respectively.
> Why?
>
In my brief testing, when run against "<tag/" it first matches "<tag" -
then the next match is "/". The second match matches "/" because it
matches the \S character class.
Post some examples of how you want the regex to behave, and maybe
someone can help put one together.
mikeb
> In my brief testing, when run against "<tag/" it first matches "<tag" -
> then the next match is "/". The second match matches "/" because it
> matches the \S character class.
But shouldn't this: [^/] stop it from doing that?
Here's how I want the regex to behave:
I want to find the first 'word' in the string. this would be any number of
characters in a row up to (but not including) a space, a new line, or a / or
>
so in this:
"hello there, how are you"
it should match 'hello'
in this:
"<blockquote>hello there, how are you"
it should match '<blockquote'
Thanks!
-Darrel

> But shouldn't this: [^/] stop it from doing that?
Aha. Mike, you are correct!
Here's what's happening. If this is my text:
<blockquote>monkey</blockquote>
and this is my Regex:
\S[^>]*
It returns these matches:
<blockquote
>monkey</blockquote
>
So, it's returning the last match, I suppose. This is where I get lost. How
do I get it to ONLY return the first match?
Got it!
The problem was the very next group I was using.
I had this:
(\S[^\s/>]*)
but had to add another group:
(\s|\n[^\S>]*)|(> ))
which checks for whitespace/new lines OR a closing tag.
-Darrel
Use the Match Class of the regular expression object
Dim m as Match = yourRegEx.Match(string)
m will return the first match
"darrel" wrote:

>
> Aha. Mike, you are correct!
> Here's what's happening. If this is my text:
> <blockquote>monkey</blockquote>
> and this is my Regex:
> \S[^>]*
> It returns these matches:
> <blockquote
> So, it's returning the last match, I suppose. This is where I get lost. Ho
w
> do I get it to ONLY return the first match?
>
>
>

stuck on a REGEX (\S[^\s/>]*)

I'm trying to find the opening < and the text of a tag (without the
attributes or closing tags)

This is what I'm using:

(\S[^\s/>]*)

Which, I think, reads as:

(any number of non-whitespace characters [up to a space, /, or >])

Is that correct? I can't get it to work.

If my text is:

<tag

then it returns "<tag" which is what I want.

However, if I have:

<tag/ or <tag
it instead matches "/" or ">" respectively.

Why?darrel wrote:
> I'm trying to find the opening < and the text of a tag (without the
> attributes or closing tags)
> This is what I'm using:
> (\S[^\s/>]*)
> Which, I think, reads as:
> (any number of non-whitespace characters [up to a space, /, or >])
> Is that correct? I can't get it to work.
> If my text is:
> <tag
> then it returns "<tag" which is what I want.
> However, if I have:
> <tag/ or <tag>
> it instead matches "/" or ">" respectively.
> Why?

In my brief testing, when run against "<tag/" it first matches "<tag" -
then the next match is "/". The second match matches "/" because it
matches the \S character class.

Post some examples of how you want the regex to behave, and maybe
someone can help put one together.

--
mikeb
> In my brief testing, when run against "<tag/" it first matches "<tag" -
> then the next match is "/". The second match matches "/" because it
> matches the \S character class.

But shouldn't this: [^/] stop it from doing that?

Here's how I want the regex to behave:

I want to find the first 'word' in the string. this would be any number of
characters in a row up to (but not including) a space, a new line, or a / or

so in this:

"hello there, how are you"

it should match 'hello'

in this:

"<blockquote>hello there, how are you"

it should match '<blockquote'

Thanks!

-Darrel
> But shouldn't this: [^/] stop it from doing that?

Aha. Mike, you are correct!

Here's what's happening. If this is my text:

<blockquote>monkey</blockquote
and this is my Regex:

\S[^>]*

It returns these matches:

<blockquote
>monkey</blockquote

So, it's returning the last match, I suppose. This is where I get lost. How
do I get it to ONLY return the first match?
Got it!

The problem was the very next group I was using.

I had this:

(\S[^\s/>]*)
but had to add another group:
(\s|\n[^\S>]*)|(>))
which checks for whitespace/new lines OR a closing tag.
-Darrel
Use the Match Class of the regular expression object
Dim m as Match = yourRegEx.Match(string)
m will return the first match

"darrel" wrote:

> > But shouldn't this: [^/] stop it from doing that?
> Aha. Mike, you are correct!
> Here's what's happening. If this is my text:
> <blockquote>monkey</blockquote>
> and this is my Regex:
> \S[^>]*
> It returns these matches:
> <blockquote
> >monkey</blockquote
> So, it's returning the last match, I suppose. This is where I get lost. How
> do I get it to ONLY return the first match?
>
>