Showing posts with label stripping. Show all posts
Showing posts with label stripping. Show all posts

Saturday, March 31, 2012

String Stripping

Hi,

I am trying to create a page that will check user input against a value in database. This page will have a text box for user to enter serial #.
Serial # length is variable but last 3 digit is always number.
For examples:
W04051234-PAY001 in this example code is 1234-PAY
W05051234-PAT002 in this example code is 1234-PAT

A12056040-G001 in this example code is 6040-G
B11056080-SU001 in this example code is 6080-s

Which is the best approach to extract CODE from user input?

Any help will be appriciated.

Thank

TekinUse the Regular Expression class, RegExp with a regular expression that looks for:
4 digits (or is it 3?) followed by
dash
followed by 1 or letters

That expression is:
\d{4}\-[A-Za-z]+
Actually,

I would like to Trancate first 5 digits and last 3 digits.

For examples:
W04051234-PAY001 in this example code is 1234-PAY
If you are attempting to strip a specific group of characters, the String class has numerous useful methods. I encourage you to read about the String class in the .net docs. I think the SubString() and Length() methods will be useful.

Saturday, March 24, 2012

Strip space before page rendering

Is there any benefit to stripping all the space from the page before
rendering by overriding the page render and using the htmltextwriter and
stringbuilder to strip linefeeds, tabs and extra space etc. I have noticed
that may site's source code is like this. Is this because it is better for
the browser or harder for someone to be able to read it. Or maybe there is a
method or command to do this automatically?

Any thoughts are appreciated.

Mikesaves bandwidth mainly...

"vMike" <Michael.George@.gewarren.com.nospam> wrote in message
news:bnpsnk$rfa$1@.ngspool-d02.news.aol.com...
> Is there any benefit to stripping all the space from the page before
> rendering by overriding the page render and using the htmltextwriter and
> stringbuilder to strip linefeeds, tabs and extra space etc. I have noticed
> that may site's source code is like this. Is this because it is better for
> the browser or harder for someone to be able to read it. Or maybe there is
a
> method or command to do this automatically?
> Any thoughts are appreciated.
> Mike
I don't think there is a way to do it automatically, but it's simple enough
to implement.

This is the approach I use:

I have a base class derived from System.Web.UI.Page and override Render

namespace DoNot.Invade.MySpace
{
class Page : System.Web.UI.Page
{
protected override void Render(HtmlTextWriter writer)
{
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
HtmlTextWriter hw = new HtmlTextWriter(sw);

base.Render (hw);

string html = sb.ToString();

html = html.Replace(Environment.NewLine, string.Empty);
html = html.Replace("\n", string.Empty); // This may be redundant
html = html.Replace("\t", string.Empty);

writer.Write(html);
}
}
}

Hope this helps
Brian W

"vMike" <Michael.George@.gewarren.com.nospam> wrote in message
news:bnpsnk$rfa$1@.ngspool-d02.news.aol.com...
> Is there any benefit to stripping all the space from the page before
> rendering by overriding the page render and using the htmltextwriter and
> stringbuilder to strip linefeeds, tabs and extra space etc. I have noticed
> that may site's source code is like this. Is this because it is better for
> the browser or harder for someone to be able to read it. Or maybe there is
a
> method or command to do this automatically?
> Any thoughts are appreciated.
> Mike
Thanks, I had something similar to that. The only thing I had to do was do
remove the <!-- that asp puts before the javascipt for postback because when
I removed the rest of the line is caused an error. But I wonder is stripping
the space make the page display any quicker in the browser as I image there
is a small amount of overhead on the server side.

"Brian W" <brianw@.gold_death_2_spam_rush.com> wrote in message
news:%23zf8QjxnDHA.1488@.TK2MSFTNGP12.phx.gbl...
> I don't think there is a way to do it automatically, but it's simple
enough
> to implement.
> This is the approach I use:
> I have a base class derived from System.Web.UI.Page and override Render
> namespace DoNot.Invade.MySpace
> {
> class Page : System.Web.UI.Page
> {
> protected override void Render(HtmlTextWriter writer)
> {
> StringBuilder sb = new StringBuilder();
> StringWriter sw = new StringWriter(sb);
> HtmlTextWriter hw = new HtmlTextWriter(sw);
> base.Render (hw);
> string html = sb.ToString();
> html = html.Replace(Environment.NewLine, string.Empty);
> html = html.Replace("\n", string.Empty); // This may be redundant
> html = html.Replace("\t", string.Empty);
> writer.Write(html);
> }
> }
> }
> Hope this helps
> Brian W
> "vMike" <Michael.George@.gewarren.com.nospam> wrote in message
> news:bnpsnk$rfa$1@.ngspool-d02.news.aol.com...
> > Is there any benefit to stripping all the space from the page before
> > rendering by overriding the page render and using the htmltextwriter and
> > stringbuilder to strip linefeeds, tabs and extra space etc. I have
noticed
> > that may site's source code is like this. Is this because it is better
for
> > the browser or harder for someone to be able to read it. Or maybe there
is
> a
> > method or command to do this automatically?
> > Any thoughts are appreciated.
> > Mike
Brian W wrote:

> I don't think there is a way to do it automatically, but it's simple enough
> to implement.
> This is the approach I use:
> I have a base class derived from System.Web.UI.Page and override Render
> namespace DoNot.Invade.MySpace
> {
> class Page : System.Web.UI.Page
> {
> protected override void Render(HtmlTextWriter writer)
> {
> StringBuilder sb = new StringBuilder();
> StringWriter sw = new StringWriter(sb);
> HtmlTextWriter hw = new HtmlTextWriter(sw);
> base.Render (hw);
> string html = sb.ToString();
> html = html.Replace(Environment.NewLine, string.Empty);
> html = html.Replace("\n", string.Empty); // This may be redundant
> html = html.Replace("\t", string.Empty);
> writer.Write(html);
> }
> }
> }

You'll want to be careful with simplistic code like this. There are
times when whitespace and newlines are significant, such as text inside
of a <pre> element or script code.

> Hope this helps
> Brian W
> "vMike" <Michael.George@.gewarren.com.nospam> wrote in message
> news:bnpsnk$rfa$1@.ngspool-d02.news.aol.com...
>>Is there any benefit to stripping all the space from the page before
>>rendering by overriding the page render and using the htmltextwriter and
>>stringbuilder to strip linefeeds, tabs and extra space etc. I have noticed
>>that may site's source code is like this. Is this because it is better for
>>the browser or harder for someone to be able to read it. Or maybe there is
> a
>>method or command to do this automatically?
>>
>>Any thoughts are appreciated.
>>
>>Mike
>>
>>

--
mikeb
True, I guess I should have mentioned that. Since the site I'm currently
working on doesn't use <pre> I don't worry about it. And what I have written
can be expanded upon to handle these situations.

There are other cases where this is a problem too. Such as the following:

<p>
This is
some text
</p
will produce the following output:

This issome text

For me, though, I just edit the html and make sure there is also a space
before the newline.

Regards
Brian W

"mikeb" <mailbox.google@.mailnull.com> wrote in message
news:%23YdMB$xnDHA.1672@.TK2MSFTNGP09.phx.gbl...
> Brian W wrote:
> > I don't think there is a way to do it automatically, but it's simple
enough
> > to implement.
> > This is the approach I use:
> > I have a base class derived from System.Web.UI.Page and override Render
> > namespace DoNot.Invade.MySpace
> > {
> > class Page : System.Web.UI.Page
> > {
> > protected override void Render(HtmlTextWriter writer)
> > {
> > StringBuilder sb = new StringBuilder();
> > StringWriter sw = new StringWriter(sb);
> > HtmlTextWriter hw = new HtmlTextWriter(sw);
> > base.Render (hw);
> > string html = sb.ToString();
> > html = html.Replace(Environment.NewLine, string.Empty);
> > html = html.Replace("\n", string.Empty); // This may be redundant
> > html = html.Replace("\t", string.Empty);
> > writer.Write(html);
> > }
> > }
> > }
> You'll want to be careful with simplistic code like this. There are
> times when whitespace and newlines are significant, such as text inside
> of a <pre> element or script code.
>
> > Hope this helps
> > Brian W
> > "vMike" <Michael.George@.gewarren.com.nospam> wrote in message
> > news:bnpsnk$rfa$1@.ngspool-d02.news.aol.com...
> >>Is there any benefit to stripping all the space from the page before
> >>rendering by overriding the page render and using the htmltextwriter and
> >>stringbuilder to strip linefeeds, tabs and extra space etc. I have
noticed
> >>that may site's source code is like this. Is this because it is better
for
> >>the browser or harder for someone to be able to read it. Or maybe there
is
> > a
> >>method or command to do this automatically?
> >>
> >>Any thoughts are appreciated.
> >>
> >>Mike
> >>
> >>
> --
> mikeb

Stripping a Currency Symbol

I'm building a data-entry form which pulls several "money" data type fields from a database.
Formatting the data in my datagrid using {0:c} is the easy part, but the database rejects the currency symbols when I try to re-insert them into the database.

I've read several threads that say to use a separate "hidden" field to hold the unformatted data, but that doesn't help since the user is entering data using the currency symbol and I'd have to update the hidden field first anyway!!

Another option I'm hearing is to "strip off the currency symbol" before submitting the form to the database.

Can someone show me some code samples demonstrating "stripping" a string? I suppose the optimal way to do this would be to check for (and strip, if found) a non-numeric character at the beginning of the string, but I have no idea what the code syntax would be.

Thanks in advance...Hello, you can always store data in database using any type of characters, have you tried to use the field of currency in the database as a TEXT field, then you can store whatever data or sybmols you want in that field,
also, if you are using ms access, you might set at the table creation the type of a field to be currency and store data in it, without having to put the $ sign for example, it will directly add thaqt symbol to it!!!! but the easiet to use a Text field and don't worry about symbols.

also, in the datagrid, you migth use a templatecolumn, that you can render the way youwant to place the currency as you like,.

hope i was able to help..
best of luck.
Unfortunately I don't get to choose the datatype in the sql server tables, I have to code against what the DBA gives me!

I really just need code samples of how to test for a non-numeric character at the beginning of a string, and then strip it off it it finds one.

Thanks anyway!
string yourstring = "$50";
char firstchar = yourstring[0];
if (!( ((int)firstchar >= (int)'0') && (int)firstchar <= (int)'9') )) {
// bad, bad first char! ;)
}

Stripping characters...

I'm trying to strip the characters from a text box...
but I only know how to apply it to text...
for example..

string fname = @dotnet.itags.org."*\John Doe";
string pattern = @dotnet.itags.org."^.*\\";
string name = Regex.Replace(fname, pattern," ");

but how can i do it for a passed variable...
like @dotnet.itags.org.Request.Form["fname"];

string fname = @dotnet.itags.org.Request.Form["fname"];
string pattern = @dotnet.itags.org."^.*\\";
string name = Regex.Replace(fname, pattern," ");

--
Sent via .NET Newsgroups
http://www.dotnetnewsgroups.comHave you tried

string pattern = @."^.*\\";
string name = Regex.Replace(Request.Form["fname"], pattern," ");

--
Regards

John Timney
Microsoft MVP

"VJ" <vncntj@.hotmail.com> wrote in message
news:%23jugUr6GGHA.532@.TK2MSFTNGP15.phx.gbl...
> I'm trying to strip the characters from a text box...
> but I only know how to apply it to text...
> for example..
>
> string fname = @."*\John Doe";
> string pattern = @."^.*\\";
> string name = Regex.Replace(fname, pattern," ");
>
> but how can i do it for a passed variable...
> like @.Request.Form["fname"];
> string fname = @.Request.Form["fname"];
> string pattern = @."^.*\\";
> string name = Regex.Replace(fname, pattern," ");
> --
> Sent via .NET Newsgroups
> http://www.dotnetnewsgroups.com

Stripping characters...

I'm trying to strip the characters from a text box...
but I only know how to apply it to text...
for example..
string fname = @dotnet.itags.org."*\John Doe";
string pattern = @dotnet.itags.org."^.*\\";
string name = Regex.Replace(fname, pattern," ");
but how can i do it for a passed variable...
like @dotnet.itags.org.Request.Form["fname"];
string fname = @dotnet.itags.org.Request.Form["fname"];
string pattern = @dotnet.itags.org."^.*\\";
string name = Regex.Replace(fname, pattern," ");
Sent via .NET Newsgroups
http://www.dotnetnewsgroups.comHave you tried
string pattern = @."^.*\\";
string name = Regex.Replace(Request.Form["fname"], pattern," ");
Regards
John Timney
Microsoft MVP
"VJ" <vncntj@.hotmail.com> wrote in message
news:%23jugUr6GGHA.532@.TK2MSFTNGP15.phx.gbl...
> I'm trying to strip the characters from a text box...
> but I only know how to apply it to text...
> for example..
>
> string fname = @."*\John Doe";
> string pattern = @."^.*\\";
> string name = Regex.Replace(fname, pattern," ");
>
> but how can i do it for a passed variable...
> like @.Request.Form["fname"];
> string fname = @.Request.Form["fname"];
> string pattern = @."^.*\\";
> string name = Regex.Replace(fname, pattern," ");
> --
> Sent via .NET Newsgroups
> http://www.dotnetnewsgroups.com

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 punctuation marks

Is there an easy way to edit a string so that only alphabetical characters
and numbers are allowed? Thanks for your help.Try this function:

Public Function Removes(ByVal mystring As String) As String

Dim newstring As String

For Each character As Char In mystring

If Not Char.IsPunctuation(character) Then newstring &= character

Next

Return newstring

End Function

This basically loops through the string and tests whether each character is
punctuation. If it is not, it appends it to a new string. You can use the
same technique for the other Is... methods of the Char type. You can also
use the Replace method to replace all instances of a specific character with
a zero-length String as in the following:

mystring.Replace("."c,"")

The lower-case c in the code tells VB.NET to interpret the String as a Char.
Try whichever one of these works best for you, if you need more help feel
free to ask. Good Luck!
--
Nathan Sokalski
njsokalski@.hotmail.com
http://www.nathansokalski.com/

"dew" <dew@.yahoo.com> wrote in message
news:uRzn4W3KGHA.720@.TK2MSFTNGP14.phx.gbl...
> Is there an easy way to edit a string so that only alphabetical characters
> and numbers are allowed? Thanks for your help.

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 Needless Data From HttpContext.Current.Request.Form

I found this nice example of printing data to the default printer of the server when the submit button is clicked. But not sure how to strip out the names of the controls, etc.

Here is the code for those interested:

1Private Sub pd_PrintPage(ByVal sender As Object, ByVal ev As PrintPageEventArgs)23 Dim yPos As Single = 2504 Dim leftMargin As Single = ev.MarginBounds.Left5 Dim topMargin As Single = ev.MarginBounds.Top6 Dim printFont = New Font("Arial", 10)78 Dim sb As StringBuilder = New StringBuilder()910 ' Page title and date/time.11 sb.Append("Maintenance Request")12 sb.Append(Environment.NewLine)13 sb.Append("DateTime: " + DateTime.Now.ToString() + Environment.NewLine)1415 ' Iterate submitted form fields and get field names.16 Dim fieldValue As String17 Dim fieldName As String1819 ' Exclude viewstate and submit button.20 For Each fieldName In HttpContext.Current.Request.Form21 Response.Write("Field Name: " & HttpContext.Current.Request.Form(fieldName) & "<br>")22 If fieldName = "__VIEWSTATE" Or fieldName = "Submit" Then2324 Else25 ' Get the field values.2627 fieldValue = HttpContext.Current.Request.Form(fieldName)2829 ' Add the field names and values to the page.30 ' Break the field values into 50 character segments so it will fit on the paper.31 ' Currently, this only accounts for fields of l50 characters or less.32 ' ISSUE: breaks in the middle of words instead of spaces3334 If fieldValue.Length > 100 Then35 sb.Append(fieldName + ": " + fieldValue.Substring(0, 50) + Environment.NewLine)36 sb.Append(" " + fieldValue.Substring(50, 50) + Environment.NewLine)37 sb.Append(" " + fieldValue.Substring(100, fieldValue.Length - 100) + Environment.NewLine)3839 ElseIf fieldValue.Length > 50 Then40 sb.Append(fieldName + ": " + fieldValue.Substring(0, 50) + Environment.NewLine)41 sb.Append(" " + fieldValue.Substring(50, fieldValue.Length - 50) + Environment.NewLine)42 Else43 sb.Append(fieldName + ": " + fieldValue + Environment.NewLine)4445 End If4647 End If4849 Next50 ev.Graphics.DrawString(sb.ToString(), printFont, Brushes.Black, leftMargin, yPos, New StringFormat())5152 End Sub
 
There has to be a better way. Any ideas?
Figured it out. Instead of using HttpContext.Current.Request.Form, I am just using the explicit names of the controls and appending the values to the stringbuilder. If anyone needs help with this, please feel free to contact me.Big Smile

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

Stripping out unwanted characters

How can I strip out unwanted characters in a string before updating the
database? For instance, in names & addresses in our client table, we want
only letters and numbers, no punctuation. Is there a way to do this?HI,

Use regular expression to remove unwanted characters and then send the
string to database. Here is a sample code to remove non-alphanumerical
characters from a string.

hoep this will help you...

public static void Main()

{

/*Reg expression to find non-alphanumeric characters*/

string pattern = @."[^A-Za-z0-9]";

/*include System.Text.RegularExpressions name space*/

Regex rgx = new Regex(pattern);

string inputStr = "ab$!Cd&%$gf!";

/*Replace non-alphanumeric characters with space*/

string outputStr = rgx.Replace(inputStr, " ");

Console.WriteLine("Output string:"+ outputStr);

}

Cheers
Vinu

"et" <eagletender2001@.yahoo.com> wrote in message
news:ODVkXK7NGHA.668@.TK2MSFTNGP11.phx.gbl...
> How can I strip out unwanted characters in a string before updating the
> database? For instance, in names & addresses in our client table, we want
> only letters and numbers, no punctuation. Is there a way to do this?
Function StripUnwantedChars(ByVal StringToStrip AsString) AsString
Dim stripped AsString
If StringToStrip <> "" Then
stripped = Regex.Replace(StringToStrip, "<(.|\n)+?>", String.Empty)
Return stripped
Else
Return ""
EndIf
EndFunction

Note : insert all the characters you want to strip inside the quotes.

For example, instead of :
stripped = Regex.Replace(StringToStrip, "<(.|\n)+?>", String.Empty)

use :
stripped = Regex.Replace(StringToStrip, "<!#(.|\n)+?>", String.Empty)

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/
===================================
"et" <eagletender2001@.yahoo.com> wrote in message news:ODVkXK7NGHA.668@.TK2MSFTNGP11.phx.gbl...
> How can I strip out unwanted characters in a string before updating the database? For instance,
> in names & addresses in our client table, we want only letters and numbers, no punctuation. Is
> there a way to do this?
Thanks, everyone, that's exactly what I was looking for.

"et" <eagletender2001@.yahoo.com> wrote in message
news:ODVkXK7NGHA.668@.TK2MSFTNGP11.phx.gbl...
> How can I strip out unwanted characters in a string before updating the
> database? For instance, in names & addresses in our client table, we want
> only letters and numbers, no punctuation. Is there a way to do this?

Stripping out unwanted characters

How can I strip out unwanted characters in a string before updating the
database? For instance, in names & addresses in our client table, we want
only letters and numbers, no punctuation. Is there a way to do this?HI,
Use regular expression to remove unwanted characters and then send the
string to database. Here is a sample code to remove non-alphanumerical
characters from a string.
hoep this will help you...
public static void Main()
{
/*Reg expression to find non-alphanumeric characters*/
string pattern = @."[^A-Za-z0-9]";
/*include System.Text.RegularExpressions name space*/
Regex rgx = new Regex(pattern);
string inputStr = "ab$!Cd&%$gf!";
/*Replace non-alphanumeric characters with space*/
string outputStr = rgx.Replace(inputStr, " ");
Console.WriteLine("Output string:"+ outputStr);
}
Cheers
Vinu
"et" <eagletender2001@.yahoo.com> wrote in message
news:ODVkXK7NGHA.668@.TK2MSFTNGP11.phx.gbl...
> How can I strip out unwanted characters in a string before updating the
> database? For instance, in names & addresses in our client table, we want
> only letters and numbers, no punctuation. Is there a way to do this?
>
Function StripUnwantedChars(ByVal StringToStrip AsString) AsString
Dim stripped AsString
If StringToStrip <> "" Then
stripped = Regex.Replace(StringToStrip, "<(.|\n)+?>", String.Empty)
Return stripped
Else
Return ""
EndIf
EndFunction
Note : insert all the characters you want to strip inside the quotes.
For example, instead of :
stripped = Regex.Replace(StringToStrip, "<(.|\n)+?>", String.Empty)
use :
stripped = Regex.Replace(StringToStrip, "<!#(.|\n)+?>", String.Empty)
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/
===================================
"et" <eagletender2001@.yahoo.com> wrote in message news:ODVkXK7NGHA.668@.TK2MSFTNGP11.phx.gbl
..
> How can I strip out unwanted characters in a string before updating the da
tabase? For instance,
> in names & addresses in our client table, we want only letters and numbers
, no punctuation. Is
> there a way to do this?
>
Thanks, everyone, that's exactly what I was looking for.
"et" <eagletender2001@.yahoo.com> wrote in message
news:ODVkXK7NGHA.668@.TK2MSFTNGP11.phx.gbl...
> How can I strip out unwanted characters in a string before updating the
> database? For instance, in names & addresses in our client table, we want
> only letters and numbers, no punctuation. Is there a way to do this?
>

Stripping out punctuation marks

Is there an easy way to edit a string so that only alphabetical characters
and numbers are allowed? Thanks for your help.Try this function:
Public Function Removes(ByVal mystring As String) As String
Dim newstring As String
For Each character As Char In mystring
If Not Char.IsPunctuation(character) Then newstring &= character
Next
Return newstring
End Function
This basically loops through the string and tests whether each character is
punctuation. If it is not, it appends it to a new string. You can use the
same technique for the other Is... methods of the Char type. You can also
use the Replace method to replace all instances of a specific character with
a zero-length String as in the following:
mystring.Replace("."c,"")
The lower-case c in the code tells VB.NET to interpret the String as a Char.
Try whichever one of these works best for you, if you need more help feel
free to ask. Good Luck!
--
Nathan Sokalski
njsokalski@.hotmail.com
http://www.nathansokalski.com/
"dew" <dew@.yahoo.com> wrote in message
news:uRzn4W3KGHA.720@.TK2MSFTNGP14.phx.gbl...
> Is there an easy way to edit a string so that only alphabetical characters
> and numbers are allowed? Thanks for your help.
>

Stripping the time portion from date field

Hi all
I've got a textbox on the web page that captures a date.
When an insert is done, the information captured into the database is of the format dd/mm/yyyy hh:mm:ss.
How do I strip the time portion and save only the date part into the database ?
I'm using a formview, sqldatasource controls for this web page.
I tried the following in formview iteminserting event:
Dim culture_2 As System.Globalization.CultureInfo = New CultureInfo("en-GB", True)
Dim wAcceptanceD As TextBox = CType(FormView1.Row.FindControl("dtAcceptDTextBox"), TextBox)
e.Values.Add("dtAcceptD", DateTime.Parse(wAcceptanceD.Text, culture_2, DateTimeStyles.NoCurrentDateDefault))

When I run the web page, I get the following error:

System.ArgumentException was unhandled by user code
Message="Item has already been added. Key in dictionary: 'dtAcceptD' Key being added: 'dtAcceptD'"
Source="mscorlib"
StackTrace:
at System.Collections.Hashtable.Insert(Object key, Object nvalue, Boolean add)

at System.Collections.Hashtable.Add(Object key, Object value)

at System.Collections.Specialized.OrderedDictionary.Add(Object key, Object value)

at Default3.FormView1_ItemInserting(Object sender, FormViewInsertEventArgs e) in C:\SRS\Default3.aspx.vb:line 12

at System.Web.UI.WebControls.FormView.OnItemInserting(FormViewInsertEventArgs e)

at System.Web.UI.WebControls.FormView.HandleInsert(String commandArg, Boolean causesValidation)

at System.Web.UI.WebControls.FormView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup)

at System.Web.UI.WebControls.FormView.OnBubbleEvent(Object source, EventArgs e)

at System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)

at System.Web.UI.WebControls.FormViewRow.OnBubbleEvent(Object source, EventArgs e)

at System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)

at System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e)

at System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument)

at System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)

at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)

at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)

at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

Can someone pleaseeeeeeeeeeeeeeeee tell me what's wrong ?
TIA.

The problem here is not the date parsing. Well, it is a problem too but not the one you're seeing.
You're using always the same key in your dictionary: "dtAcceptD". The first time it's ok, the second time, it crashes with the error you're seeing. A dictionary can have only one entry with a given key.
As for date parsing, you should specify the date format you're expecting explicitly. There is an overload of ParseExact that takes a format string.
Once you've gotten a DateTime object, it's easy to strip the time part (but why are you asking for it if you're going to throw it away? ) by constructing a new date object from the parts of the old one, something like:
new DateTime(myDate.Year, myDate.Month, myDate.Day)

Hi
Thanks for your reply.
Realised the following:
1) SQL saves the information for a datetime field with date and time information.
2) Viewing the information from the following sources have different results:
From SQL Enterprise Manager, information displayed as dd/mm/yyy.
From SQL Query Analyzer, information displayed as yyyy-mm-dd 00:00:00.
From VS (via the Database Explorer), its displayed as dd/mm/yyyy 12:00:00.
I probably dont need to do any conversion in the ItemInserting event of the formview.
Thanks for the tip on stripping the time portion of a datetime object. I'm sure it will come in handy.

Stripping the format of files.

Hi Guys,

I am fairly new to asp.net but am embarking on a project based on asp.net I am wondering if anyone has any experience or knowledge on how to strip the formatting of different documents? Maybe for a start, how do you strip the formatting of styles and stuff in MS Words to just purely text?

Regards,

Raymond

Hi,

if you mean that you just want to get the text itself and not the colors, ... you can copy paste it into notepad and from notepad copy paste to somewhere else.

Grz, Kris.


Hi Kris,

Thanks for your input. Actually I am looking more at the coding level because I need to strip the formatting of the file automatically before I can do further comparing of the contents in string or text format.

Regards,
Raymond


Is there anyone with the relevant experience who can share their knowledge?

Thanks!

Raymond

Stripping text files

Hi all,

I'm trying to have my ASP.NET application read in a text file, and put the characters into a string.

I realized that there are some weird symbols (a square) between the texts - I think it's either a carriage return or a newline character, and want to take them out by the Trim method.

Does anybody know how I can remove those symbols? Thanks!You can use this overload

public string Trim(params char[]);
, if you know which characters you want to remove.
Most likley candidates are chr(10) (carriage return) followed by chr(13) line feed, especiallly if you are working with report files or anything unix based.

Regards

Jeff............