Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP General > Replace in ASP/VBS/SQL

Reply
Thread Tools

Replace in ASP/VBS/SQL

 
 
Pret Orian
Guest
Posts: n/a
 
      07-08-2003
I need to replace all occurancies of "is" in "This is a test (is), is,
penis." with "<a href=is.htm>is</a>".
The thing is, if I use a simple Replace function, the "is" in penis
also gets replaced.
I need to do it either in ASP/VBS or MSSQL, but I'd have it rather
done in ASP.
TIA
 
Reply With Quote
 
 
 
 
Michael Harris \(MVP\)
Guest
Posts: n/a
 
      07-08-2003
Pret Orian wrote:
> I need to replace all occurancies of "is" in "This is a test (is), is,
> penis." with "<a href=is.htm>is</a>".
> The thing is, if I use a simple Replace function, the "is" in penis
> also gets replaced.
> I need to do it either in ASP/VBS or MSSQL, but I'd have it rather
> done in ASP.
> TIA



s1 = "This is his test (is), is, is."
with new regexp
.global = true
.ignorecase = true
.pattern = "\bis\b"
s2 = .replace(s1,"<a href=is.htm>is</a>")
end with

msgbox s1 & vbcrlf & s2


--
Michael Harris
Microsoft.MVP.Scripting
Seattle WA US

Technet Script Center
http://www.microsoft.com/technet/scr...er/default.asp

Microsoft® Windows®2000 Scripting Guide
http://www.microsoft.com/technet/scr...s_overview.asp

 
Reply With Quote
 
 
 
 
Pret Orian
Guest
Posts: n/a
 
      07-09-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) (Pret Orian) wrote in message news:<(E-Mail Removed). com>...
> I need to replace all occurancies of "is" in "This is a test (is), is,
> penis." with "<a href=is.htm>is</a>".
> The thing is, if I use a simple Replace function, the "is" in penis
> also gets replaced.
> I need to do it either in ASP/VBS or MSSQL, but I'd have it rather
> done in ASP.
> TIA


I'm afraid I didn't explain my problem god enough.
I want to replace "is" only if it is not a part of another word, so
something like [^a-zA-Z]is[^a-zA-Z] pattern. "(is)", "is," should all
be positive matches, and "penis" should not.
 
Reply With Quote
 
Joe Earnest
Guest
Posts: n/a
 
      07-09-2003

"Pret Orian" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> I'm afraid I didn't explain my problem god enough.
> I want to replace "is" only if it is not a part of another word, so
> something like [^a-zA-Z]is[^a-zA-Z] pattern. "(is)", "is," should all
> be positive matches, and "penis" should not.


Did you try Michael Harris' script? (He politely uses "his" in his example
to demonstrate.)

Joe Earnest


 
Reply With Quote
 
Pret Orian
Guest
Posts: n/a
 
      07-09-2003
>Did you try Michael Harris' script? (He politely uses "his" in his
example
>to demonstrate.)


I did try the Michael's script and it did work indeed (thanx Michael).
However, I'm having a problem with "is" word standing on the beginning
of the string (or at the end). What would be a pattern that would
exclude all the characters that are not a-zA-Z except if beeing on the
beginning of the string
For example:

..pattern = "\bis\b"

Replaces: "this is something" "(is)" " This is." ...
But leaves "is this something" untouched.

Is there a way out?
Where are the escape characters (like \b) documented?
regards
 
Reply With Quote
 
Joe Earnest
Guest
Posts: n/a
 
      07-09-2003
Hi,

"Pret Orian" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> I did try the Michael's script and it did work indeed (thanx Michael).
> However, I'm having a problem with "is" word standing on the beginning
> of the string (or at the end). What would be a pattern that would
> exclude all the characters that are not a-zA-Z except if beeing on the
> beginning of the string
> For example:
>
> .pattern = "\bis\b"
>
> Replaces: "this is something" "(is)" " This is." ...
> But leaves "is this something" untouched.
>
> Is there a way out?
> Where are the escape characters (like \b) documented?
> regards


I'm not much at regex, and maybe some one can post a simple regex addition,
but as a fallback, you could do the beginning and end issues with standard
string manipulation, after using Michael's regex script. It's kludgy, but
it works.

s1= "is this something? it is, it is"

' Michael's regex script
with new regexp
.global = true
.ignorecase = true
.pattern = "\bis\b"
s2 = .replace(s1,"<a href=is.htm>is</a>")
end with

'add
if lcase(left(s2 & " ", 3)="is ") then _
s2= "<a href=is.htm>is</a>" & mid(s2, 3)
if lcase(right(s2, 3)=" is") then _
s2= left(s2, len(s2) -2) & "<a href=is.htm>is</a>"

msgbox s1 & vbcrlf & s2

Joe Earnest



 
Reply With Quote
 
Michael Harris \(MVP\)
Guest
Posts: n/a
 
      07-09-2003
Pret Orian wrote:
>> Did you try Michael Harris' script? (He politely uses "his" in his
>> example to demonstrate.)

>
> I did try the Michael's script and it did work indeed (thanx Michael).
> However, I'm having a problem with "is" word standing on the beginning
> of the string (or at the end). What would be a pattern that would
> exclude all the characters that are not a-zA-Z except if beeing on the
> beginning of the string


s1 = "Is this what his is_test is? - Is IS iS - yes, it is"
with new regexp
.global = true
.ignorecase = true
.pattern = "\W(is)\W"
s2 = .replace(s1,"<a href=is.htm>is</a>")
end with

msgbox s1 & vbcrlf & s2

--
Michael Harris
Microsoft.MVP.Scripting
Seattle WA US

Technet Script Center
http://www.microsoft.com/technet/scr...er/default.asp

Microsoft® Windows®2000 Scripting Guide
http://www.microsoft.com/technet/scr...s_overview.asp

 
Reply With Quote
 
Chris Hohmann
Guest
Posts: n/a
 
      07-10-2003
"Michael Harris (MVP)" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> What I actually meant was this...notice the $1 in the replacement

string to
> use the text that was matched in the replacement text...
>
> s1 = "Is this what his is_test is? - Is IS iS - yes, it is"
> with new regexp
> .global = true
> .ignorecase = true
> .pattern = "\W(is)\W"
> s2 = .replace(s1,"<a href=is.htm>$1</a>")
> end with
>
> msgbox s1 & vbcrlf & s2
>
> --
> Michael Harris
> Microsoft.MVP.Scripting
> Seattle WA US
>
> Technet Script Center
> http://www.microsoft.com/technet/scr...er/default.asp
>
> Microsoft® Windows®2000 Scripting Guide
>

http://www.microsoft.com/technet/scr...s_overview.asp
>


Michael, your code consumes the surrounding "non-word" characters.
Here's some code based on the OP's original pattern
"[^a-zA-Z]is[^a-zA-Z]". I reduced the range since ignore case was in
effect.

<script language="VBScript" runat="SERVER">
str = "Is this what his is_test is? - Is IS iS - yes, it is"
with new regexp
.global = true
.ignorecase = true
.pattern = "([^a-z])(is)([^a-z])"
Response.Write "<br>VBScript: " & .replace(str,"$1<a
href=is.htm>$2</a>$3")
end with
</script>
<script language="JScript" runat="SERVER">
Response.Write("<br>JScript: " + "Is this what his is_test is? - Is IS
iS - yes, it is".replace(/([^a-z])(is)([^a-z])/gi,"$1<a
href=is.htm>$2</a>$3"));
</script>

For the OP, here are links to regular expression syntax in VBScript and
JScript respectively:
http://msdn.microsoft.com/library/en...propattern.asp
http://msdn.microsoft.com/library/en...gexpsyntax.asp


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Replace /n with a XHTML <br /> using string.replace Alun ASP .Net 3 02-18-2008 05:52 AM
Re: [Pyrex] pyrex functions to replace a method (Re: replace a method Greg Ewing Python 2 06-29-2006 05:25 PM
pyrex functions to replace a method (Re: replace a method in class:how?) Brian Blais Python 1 06-27-2006 12:13 PM
s/$match/$replace/ fails when $replace has backreferences anonyusenet@yahoo.com Perl Misc 3 09-12-2005 11:03 AM
help with string replace - for doing selective replace Prasad S Javascript 2 08-27-2004 03:22 PM



Advertisments