Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Other Programming > VBScript
|
VBScript For questions and discussions related to VBScript.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VBScript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old May 17th, 2005, 03:50 PM
Authorized User
 
Join Date: Sep 2004
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default Regular Expressions and Replace Function

My valid characters for username are letters and numbers only.
so if the username is abc*67% i want to replace the invalid chars with nothiing. so the correct result should be abc67. i get abc67%.

 I am able to replace only one invalid character. the others stay as it is . any input?

this is my code:-


    username = rtrim(rssqlusername("ag_username"))

    'Test the ag_username for valid chars
    ' Valid Characters Include Letters and Numbers only

    set Regexforinvaliddata = New RegExp
    Regexforinvaliddata.Pattern ="[^A-Za-z0-9 ]"
    set colMatches = Regexforinvaliddata.execute(username)


'Step through our matches
    for each Regexforinvaliddata in colmatches

            searchchar = Regexforinvaliddata.value

            username = replace(username, searchchar, "")
    Next

Reply With Quote
  #2 (permalink)  
Old May 18th, 2005, 05:45 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

use the replace method of regexp instead of execute
Regexforinvaliddata.replace username, ""
Reply With Quote
  #3 (permalink)  
Old May 18th, 2005, 08:28 AM
Authorized User
 
Join Date: Sep 2004
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I dont have to replace the entire username with nulls but
only the invalid characters --which i have to find with the execute method.

Reply With Quote
  #4 (permalink)  
Old May 18th, 2005, 02:00 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

you misunderstand what the regexp replace method does. It replaces all characters that match the regular expression, not all characters in the string. Try it!
Reply With Quote
  #5 (permalink)  
Old May 20th, 2005, 05:10 PM
Authorized User
 
Join Date: Jul 2004
Posts: 34
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via ICQ to chipset Send a message via MSN to chipset Send a message via Yahoo to chipset
Default

why not write a function to perform the operation
for you
here is the one i have written;

Code:
Option Explicit
Const InvalideChar As String = "~`!@#$%^&*()_-+=|\{}[];':<>,.?/"


Private Function RemoveInvalideChar(Expression As String, _
                                sInvalideChar As String)


    Dim i As Integer            'counter
    Dim sChar As String         'hold single charater
    Dim iLenChar As Integer     'lenght of all character

    iLenChar = Len(sInvalideChar)
    For i = 1 To iLenChar
        sChar = Mid$(sInvalideChar, i, 1)
        Expression = Replace(Expression, sChar, vbNullString)
    Next i
    RemoveInvalideChar = Replace(Expression, Chr(34), vbNullString) ' chr(34) is "
End Function

hope it helps

Arowolo
Reply With Quote





Similar Threads
Thread Thread Starter Forum Replies Last Post
Do I need regular expressions..? ypomonh XSLT 2 May 20th, 2007 05:09 PM
Regular Expressions mega Beginning PHP 1 February 5th, 2007 05:31 PM
Regular expressions on C# hideway C# 2 November 27th, 2006 05:08 PM
regular expressions help kyootepuffy Classic ASP Databases 2 September 10th, 2003 01:37 PM
Regular Expressions Dave Doknjas C# 1 August 9th, 2003 12:05 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.