Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP General > One line replace for multiple targets?

Reply
Thread Tools

One line replace for multiple targets?

 
 
Noozer
Guest
Posts: n/a
 
      02-03-2006
Writing some ASP code and I need to take a string, representing a phone
number, and strip out some characters, namely spaces, brackets and hyphens.

I could write

pNum=replace(pNum," ","")
pNum=replace(pNum,"-","")
pNum=replace(pNum,"(","")
pNum=replace(pNum,")","")

....but that's a bit of a pain. Especially since I'll be doing similar
replacements with other data and may have to strip out a larger number of
characters.

I know that this can be done with regular expressions, but all the examples
I see online are several lines in length. Isn't there a simple way to just
say "remove any of these" or "replace any of these with nothing" in a single
line of code?


 
Reply With Quote
 
 
 
 
Bob Barrows [MVP]
Guest
Posts: n/a
 
      02-03-2006
Noozer wrote:
> Writing some ASP code and I need to take a string, representing a
> phone number, and strip out some characters, namely spaces, brackets
> and hyphens.
> I could write
>
> pNum=replace(pNum," ","")
> pNum=replace(pNum,"-","")
> pNum=replace(pNum,"(","")
> pNum=replace(pNum,")","")
>
> ...but that's a bit of a pain. Especially since I'll be doing similar
> replacements with other data and may have to strip out a larger
> number of characters.
>
> I know that this can be done with regular expressions, but all the
> examples I see online are several lines in length. Isn't there a
> simple way to just say "remove any of these" or "replace any of these
> with nothing" in a single line of code?

No
Sorry
--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"


 
Reply With Quote
 
 
 
 
McKirahan
Guest
Posts: n/a
 
      02-03-2006
"Noozer" <(E-Mail Removed)> wrote in message
news:2xDEf.315369$tl.211704@pd7tw3no...
> Writing some ASP code and I need to take a string, representing a phone
> number, and strip out some characters, namely spaces, brackets and

hyphens.
>
> I could write
>
> pNum=replace(pNum," ","")
> pNum=replace(pNum,"-","")
> pNum=replace(pNum,"(","")
> pNum=replace(pNum,")","")
>
> ...but that's a bit of a pain. Especially since I'll be doing similar
> replacements with other data and may have to strip out a larger number of
> characters.
>
> I know that this can be done with regular expressions, but all the

examples
> I see online are several lines in length. Isn't there a simple way to just
> say "remove any of these" or "replace any of these with nothing" in a

single
> line of code?


If you only want digits then here's one approach.

<html>
<head>
<title>numbonly.htm</title>
<script type="text/javascript">
function numbonly(that) {
var numb = that.value;
var what = "";
for (var i=0; i<numb.length; i++) {
var byte = numb.substr(i,1);
if (byte >= "0" && byte <= "9") what += byte;
}
that.value = what;
}
</script>
</head>
<body>
<center>
<form name="form1">
<input type="text" name="numb" size="20" maxlength="20"
value="(123) 456-7890" onblur="numbonly(this)">
</form>
</center>
</body>
</html>


 
Reply With Quote
 
Anthony Jones
Guest
Posts: n/a
 
      02-04-2006
>>
I know that this can be done with regular expressions, but all the examples
I see online are several lines in length. Isn't there a simple way to just
say "remove any of these" or "replace any of these with nothing" in a single
line of code?
<<

As already stated no there isn't but yet there is; use a function (the art
of abstraction is often
lost in the serial nature of ASP script).

Have the following function (and a range of other useful utilities) in an
ASP page that is included in the pages where you need them:-

Function RegExReplace(rsInput, rsPattern, rsReplace)

Dim oRegEx

Set oRegEx = New RegExp

oRegEx.Pattern = rsPattern
oRegEx.Global = True
RegExReplace = oRegEx.Replace(rsInput, rsReplace)

End Function

Now your stated case becomes:-

pNum = RegExReplace(pNum;" |-|\)|\)","")

and your other similar cases will be simply a matter of varing the pattern.


PS. where is 'microsoft.public.inetserver.iis.activeserverpages ' that you
cross posted to?
my newsgroup app complains that is doesn't exist

Anthony.


 
Reply With Quote
 
McKirahan
Guest
Posts: n/a
 
      02-04-2006
"Anthony Jones" <(E-Mail Removed)> wrote in message
news:#hI#(E-Mail Removed)...

[snip]

> PS. where is 'microsoft.public.inetserver.iis.activeserverpages ' that

you
> cross posted to? my newsgroup app complains that is doesn't exist


"microsoft.public.inetserver.iis.activeserverpages "
exists and has had 588 posts since January 1, 2004.


 
Reply With Quote
 
Andyza
Guest
Posts: n/a
 
      02-16-2006
Bob, it's very rare, but this time you're wrong. See the message in
this thread by Anthony Jones. A Regular Expression is ideal for this
problem.

 
Reply With Quote
 
Bob Barrows [MVP]
Guest
Posts: n/a
 
      02-16-2006
Andyza wrote:
> Bob, it's very rare, but this time you're wrong. See the message in
> this thread by Anthony Jones. A Regular Expression is ideal for this
> problem.


Oh wait! I AM wrong. Nested Replace statements will accomplish this on a
single line.

This
> pNum=replace(pNum," ","")
> pNum=replace(pNum,"-","")
> pNum=replace(pNum,"(","")
> pNum=replace(pNum,")","")


can be done by:
pnum=replace(replace(replace(replace(pNum,")",""), "(",""),"-","")," ","")

--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"


 
Reply With Quote
 
Bob Barrows [MVP]
Guest
Posts: n/a
 
      02-16-2006
Andyza wrote:
> Bob, it's very rare, but this time you're wrong. See the message in
> this thread by Anthony Jones. A Regular Expression is ideal for this
> problem.

What are you talking about? Please quote the message to which you are
replying.
Oh wait, I remember now.

A Regular Expession may be ideal for this problem, but a regular expression
also takes at least 3 lines to use, one to instantiate the regexp object,
one to initialize the pattern, and one to execute the replace statement. So
it is not the answer to the question you asked.

--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"


 
Reply With Quote
 
Evertjan.
Guest
Posts: n/a
 
      02-16-2006
Noozer wrote on 03 feb 2006 in microsoft.public.inetserver.asp.general:

> Writing some ASP code and I need to take a string, representing a
> phone number, and strip out some characters, namely spaces, brackets
> and hyphens.
>
> I could write
>
> pNum=replace(pNum," ","")
> pNum=replace(pNum,"-","")
> pNum=replace(pNum,"(","")
> pNum=replace(pNum,")","")
>
> ...but that's a bit of a pain. Especially since I'll be doing similar
> replacements with other data and may have to strip out a larger number
> of characters.
>
> I know that this can be done with regular expressions, but all the
> examples I see online are several lines in length. Isn't there a
> simple way to just say "remove any of these" or "replace any of these
> with nothing" in a single line of code?


execute function in vbs or js:
<%
pNum = pNumReplace(pNum)
%>

define in jscript:
<script runat=server language=jscript>
function pNumReplace(x) { return x.replace(/[ -()]+/g,'') }
</script>


--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
 
Reply With Quote
 
Andyza
Guest
Posts: n/a
 
      02-16-2006
> Now your stated case becomes:-
>
> pNum = RegExReplace(pNum;" |-|\)|\)","")
>
> and your other similar cases will be simply a matter of varing the pattern.


Maybe I'm reading it wrong, but shouldn't that be:

pNum = RegExReplace(pNum," |-|\(|\)","")

i.e.: " |-|\(|\)" instead of " |-|\)|\)"

The original example has two closing brackets instead of one opening
and one closing bracket.

And shouldn't there be a comma after pNum instead of a semicolon?

 
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
Can one declare more than one signal on one line? Merciadri Luca VHDL 4 11-01-2010 02:00 PM
Read a file line by line with a maximum number of characters per line Hugo Java 10 10-18-2004 11:42 AM
CSS aligning two things on one line with one left and one right news.frontiernet.net HTML 6 04-16-2004 02:44 AM
Using One XSLT and multiple XML Problem (One is XML and another one is XBRL) loveNUNO XML 2 11-20-2003 06:47 AM
Regex - assign to new variable and replace in one line? Brad Walton Perl Misc 9 08-21-2003 09:17 PM



Advertisments