Showing posts with label regular. Show all posts
Showing posts with label regular. Show all posts

Monday, March 26, 2012

StringBuilder to Text

Hello, I a m building an email message in a StringBuilder Object and then
using the StringBuilder ToString() method to get it to regular text. It
appears to me that when I place a "\n" into the Stringbuilder object that
when this is translated into text, I loose the page break. Is this true and
I doing something wrong?
I'm guessing that I could insert a special character into the StringBuilder
object and then use the string Replace() method to translate that special
character into a "\n" and perhaps this would work.
Just wondering why it does not work when I place that value into the
StringBuilder Object.
ThanksHow are you noticing that you loose the break? In what context is this
happening?
"Jim Heavey" <JimHeavey@.discussions.microsoft.com> wrote in message
news:1BC14F11-5DBA-4865-9CFB-2881598EB955@.microsoft.com...
> Hello, I a m building an email message in a StringBuilder Object and then
> using the StringBuilder ToString() method to get it to regular text. It
> appears to me that when I place a "\n" into the Stringbuilder object that
> when this is translated into text, I loose the page break. Is this true
> and
> I doing something wrong?
> I'm guessing that I could insert a special character into the
> StringBuilder
> object and then use the string Replace() method to translate that special
> character into a "\n" and perhaps this would work.
> Just wondering why it does not work when I place that value into the
> StringBuilder Object.
> Thanks
Have you considered using Environment.Newline instead of "\n"?
Example:
StringBuilder textBuilder = new StringBuilder(512);
textBuilder.Append("Dear John,");
textBuilder.Append(Environment.Newline)
Perhaps that will help.
carl
"Jim Heavey" <JimHeavey@.discussions.microsoft.com> wrote in message
news:1BC14F11-5DBA-4865-9CFB-2881598EB955@.microsoft.com...
> Hello, I a m building an email message in a StringBuilder Object and then
> using the StringBuilder ToString() method to get it to regular text. It
> appears to me that when I place a "\n" into the Stringbuilder object that
> when this is translated into text, I loose the page break. Is this true
> and
> I doing something wrong?
> I'm guessing that I could insert a special character into the
> StringBuilder
> object and then use the string Replace() method to translate that special
> character into a "\n" and perhaps this would work.
> Just wondering why it does not work when I place that value into the
> StringBuilder Object.
> Thanks
Try Environment.NewLine
Remy
I am loosing the page break when I use the StringBuilder ToString() method
when I am formatting an email message. I will try the Environment.NewLine()
method and see if that retains the line break.
Does the StringBuilder cause the newline disappear, or is it something with
the mail component. What happens if you just set the
StringBuilder.ToString() to a variable and looked at it, is the newline
gone?
"Jim Heavey" <JimHeavey@.discussions.microsoft.com> wrote in message
news:9876DF24-F310-4FDC-826D-D7872576655C@.microsoft.com...
>I am loosing the page break when I use the StringBuilder ToString() method
> when I am formatting an email message. I will try the
> Environment.NewLine()
> method and see if that retains the line break.
>
I assume you are using c?
If you pass "\n" you'll get newline > chr(10)
If you want "\n" you might consider to use "\\n"
(Outlook express mangels this for me, use slash twice)
"Jim Heavey" <JimHeavey@.discussions.microsoft.com> schreef in bericht
news:1BC14F11-5DBA-4865-9CFB-2881598EB955@.microsoft.com...
> Hello, I a m building an email message in a StringBuilder Object and then
> using the StringBuilder ToString() method to get it to regular text. It
> appears to me that when I place a "\n" into the Stringbuilder object that
> when this is translated into text, I loose the page break. Is this true
> and
> I doing something wrong?
> I'm guessing that I could insert a special character into the
> StringBuilder
> object and then use the string Replace() method to translate that special
> character into a "\n" and perhaps this would work.
> Just wondering why it does not work when I place that value into the
> StringBuilder Object.
> Thanks

StringBuilder to Text

Hello, I a m building an email message in a StringBuilder Object and then
using the StringBuilder ToString() method to get it to regular text. It
appears to me that when I place a "\n" into the Stringbuilder object that
when this is translated into text, I loose the page break. Is this true and
I doing something wrong?

I'm guessing that I could insert a special character into the StringBuilder
object and then use the string Replace() method to translate that special
character into a "\n" and perhaps this would work.

Just wondering why it does not work when I place that value into the
StringBuilder Object.

ThanksHow are you noticing that you loose the break? In what context is this
happening?

"Jim Heavey" <JimHeavey@.discussions.microsoft.com> wrote in message
news:1BC14F11-5DBA-4865-9CFB-2881598EB955@.microsoft.com...
> Hello, I a m building an email message in a StringBuilder Object and then
> using the StringBuilder ToString() method to get it to regular text. It
> appears to me that when I place a "\n" into the Stringbuilder object that
> when this is translated into text, I loose the page break. Is this true
> and
> I doing something wrong?
> I'm guessing that I could insert a special character into the
> StringBuilder
> object and then use the string Replace() method to translate that special
> character into a "\n" and perhaps this would work.
> Just wondering why it does not work when I place that value into the
> StringBuilder Object.
> Thanks
Have you considered using Environment.Newline instead of "\n"?

Example:

StringBuilder textBuilder = new StringBuilder(512);
textBuilder.Append("Dear John,");
textBuilder.Append(Environment.Newline)

Perhaps that will help.

carl

"Jim Heavey" <JimHeavey@.discussions.microsoft.com> wrote in message
news:1BC14F11-5DBA-4865-9CFB-2881598EB955@.microsoft.com...
> Hello, I a m building an email message in a StringBuilder Object and then
> using the StringBuilder ToString() method to get it to regular text. It
> appears to me that when I place a "\n" into the Stringbuilder object that
> when this is translated into text, I loose the page break. Is this true
> and
> I doing something wrong?
> I'm guessing that I could insert a special character into the
> StringBuilder
> object and then use the string Replace() method to translate that special
> character into a "\n" and perhaps this would work.
> Just wondering why it does not work when I place that value into the
> StringBuilder Object.
> Thanks
Try Environment.NewLine

Remy
I am loosing the page break when I use the StringBuilder ToString() method
when I am formatting an email message. I will try the Environment.NewLine()
method and see if that retains the line break.
Does the StringBuilder cause the newline disappear, or is it something with
the mail component. What happens if you just set the
StringBuilder.ToString() to a variable and looked at it, is the newline
gone?

"Jim Heavey" <JimHeavey@.discussions.microsoft.com> wrote in message
news:9876DF24-F310-4FDC-826D-D7872576655C@.microsoft.com...
>I am loosing the page break when I use the StringBuilder ToString() method
> when I am formatting an email message. I will try the
> Environment.NewLine()
> method and see if that retains the line break.
I assume you are using c?

If you pass "\n" you'll get newline > chr(10)
If you want "\n" you might consider to use "\\n"
(Outlook express mangels this for me, use slash twice)

"Jim Heavey" <JimHeavey@.discussions.microsoft.com> schreef in bericht
news:1BC14F11-5DBA-4865-9CFB-2881598EB955@.microsoft.com...
> Hello, I a m building an email message in a StringBuilder Object and then
> using the StringBuilder ToString() method to get it to regular text. It
> appears to me that when I place a "\n" into the Stringbuilder object that
> when this is translated into text, I loose the page break. Is this true
> and
> I doing something wrong?
> I'm guessing that I could insert a special character into the
> StringBuilder
> object and then use the string Replace() method to translate that special
> character into a "\n" and perhaps this would work.
> Just wondering why it does not work when I place that value into the
> StringBuilder Object.
> Thanks

Saturday, March 24, 2012

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.
>>>
>>
>>

Tuesday, March 13, 2012

Stuck on a Regular Expression...!?

I have the following rule in my web.config that is used in a url redirect

<RewriterRule>

___<LookFor>~/Category/(\w+)\.aspx></LookFor>

___<SendTo><![CDATA[~/Page.aspx?m=13&i=$1]]></SendTo>

</RewriterRule>

This works fine. I'm trying to do a slightly more complex one but I'm having problems with the correct expression and was hoping someone could help.

I'm trying to set it up so it when you browse to /Category/Product/892_23.aspx

It goes to Page.aspx?m=13&c=892&i=23 (eg category 892 and item 23)

The categoryID and productID can be any number of digits, that is why I have put in the '_' so I can see where one ends and the other starts.

What I'm not sure about is how to create the expression that will split them up��

Any help would be great. Thanks

I'm writing this off the top of my head, so I may get it wrong. However, try the following:
<RewriterRule>
<LookFor>~/Category/Products/(\d+)_(\d+)\.aspx></LookFor>
<SendTo><![CDATA[~/Page.aspx?m=13&c=$1&i=$2]]></SendTo>
</RewriterRule>

Thanks for that, that worked first time...

I'm sorry to hyjack my own thread but I have one last question.

I have the following site structure:

SiteRoot/Page.aspx

Page.aspx loads other content pages (ascx) into it based on parameters passed in the querystring.

Eg. Page.aspx?m=1&i=1 would display the "News" module (moduleID=1) and news item 1 (newsID=1)

This works fine. I want to implement URLrewriting. I have followed this articlehttp://msdn.microsoft.com/library/?url=/library/en-us/dnaspp/html/urlrewriting.asp and have hit a problem.
I have set up in my web.config file rules:

<RewriterRule>
___<LookFor>~/News/(\w)\.aspx</LookFor>
___<SendTo>~/Page.aspx?m=1&i=$1</SendTo>
</RewriterRule>

This works fine so when I type MySite/News/1.aspx into the browser it rewrites to MySite/Page.aspx?m=1&i=1 and shows the page.

The problem is the images on the News Item page. They have <img src='images/image1.jpg'> this shows fine on MySite/Page.aspx?m=1&i=1 (Page.aspx is on the root). However when I go to MySite/News/1.aspx, the source for the image is broken. It is now looking for the images folder in the News folder which does not exist.

Do I have to put <img src="http://pics.10026.com/?src=~/images/image.jpg" runat='server'/> on every image reference, or is there another way around this.

Thanks again


ricc wrote:

The problem is the images on the News Item page. They have <img src='images/image1.jpg'> this shows fine on MySite/Page.aspx?m=1&i=1 (Page.aspx is on the root). However when I go to MySite/News/1.aspx, the source for the image is broken. It is now looking for the images folder in the News folder which does not exist.

Do I have to put <img src="http://pics.10026.com/?src=~/images/image.jpg" runat='server'/> on every image reference, or is there another way around this.

Yes, I approved your new thread wherein you asked the same question.
I didn't provide an answer, because frankly I don't know how other developers deal with this problem. To me, adding a tilde to the start of all links would be a royal pain. If you would like to see how I deal with this problem, please see my reply in the following thread:
UserControls and relative links