need help with a regular expression replace command

Discussion in 'ASP General' started by Danny, Oct 3, 2004.

  1. Danny

    Danny Guest

    I need an asp command to strip out from a string all extra punctuation such
    as apostrophe, comma, period, spaces dashes, etc etc and just leave the
    letters.

    Can anybody give me some ideas?

    Thanks
    Danny, Oct 3, 2004
    #1
    1. Advertising

  2. Danny

    Roland Hall Guest

    "Danny" wrote in message
    news:pwU7d.28368$...
    :I need an asp command to strip out from a string all extra punctuation such
    : as apostrophe, comma, period, spaces dashes, etc etc and just leave the
    : letters.
    :

    Yes. It's easier to match what you want and just dump the rest. This
    example returns A-Z regardless of case or in your terms, "just the letters."

    <%@ Language=VBScript %>
    <%
    Option Explicit
    Response.Buffer = True

    function getAlpha(str)
    dim re, match, matches, reStr
    set re = New RegExp
    re.Pattern = "[a-z]+"
    re.IgnoreCase = True
    re.Global = True
    set matches = re.Execute(str)
    for each match in matches
    reStr = reStr & match.value
    next
    getAlpha = reStr
    end function

    Response.Write("Result: " & getAlpha("AaB5b!'C,*cD/d\Ee"))
    %>

    Response:
    Result: AaBbCcDdEe

    HTH...

    --
    Roland Hall
    /* This information is distributed in the hope that it will be useful, but
    without any warranty; without even the implied warranty of merchantability
    or fitness for a particular purpose. */
    Technet Script Center - http://www.microsoft.com/technet/scriptcenter/
    WSH 5.6 Documentation - http://msdn.microsoft.com/downloads/list/webdev.asp
    MSDN Library - http://msdn.microsoft.com/library/default.asp
    Roland Hall, Oct 3, 2004
    #2
    1. Advertising

  3. Danny

    Evertjan. Guest

    Roland Hall wrote on 03 okt 2004 in
    microsoft.public.inetserver.asp.general:

    > "Danny" wrote in message
    > news:pwU7d.28368$...
    >:I need an asp command to strip out from a string all extra punctuation
    >:such
    >: as apostrophe, comma, period, spaces dashes, etc etc and just leave
    >: the letters.
    >:
    >
    > Yes. It's easier to match what you want and just dump the rest. This
    > example returns A-Z regardless of case or in your terms, "just the
    > letters."
    >


    I don't think so,
    the replace function is much quicker than matching and a loop:

    function getAlpha(str)
    dim re
    set re = New RegExp
    re.Pattern = "[^a-z]+"
    re.IgnoreCase = True
    re.Global = True
    getAlpha = re.replace(str,"")
    end function

    Response.Write "Result: " & getAlpha("AaB5b!'C,*cD/d\Ee")


    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress,
    but let us keep the discussions in the newsgroup)
    Evertjan., Oct 3, 2004
    #3
  4. Danny wrote:
    > I need an asp command to strip out from a string all extra
    > punctuation such as apostrophe, comma, period, spaces dashes, etc etc
    > and just leave the letters.


    JScript:
    return myString.replace(/[^a-z]/gi,"")

    VBScript:
    Use the Replace function and the RegExp object to do the same.



    --
    Dave Anderson

    Unsolicited commercial email will be read at a cost of $500 per message. Use
    of this email address implies consent to these terms. Please do not contact
    me directly or ask me to contact you directly for assistance. If your
    question is worth asking, it's worth posting.
    Dave Anderson, Oct 4, 2004
    #4
  5. Danny

    Guest

    I use a function for that. There might a slicker way to do it, but this is what I came up with. You can replace each puncuation mark with just an empty string "" instead of the encoding.

    Function EncPunc(checkPunc)
    strTmp = checkPunc
    strTmp = Replace(strTmp,"'","%39")
    strTmp = Replace(strTmp,"%","%25")
    strTmp = Replace(strTmp,"&","%26")
    strTmp = Replace(strTmp,"#","%23")
    strTmp = Replace(strTmp,";","%3B")
    strTmp = Replace(strTmp,"/","%2F")
    strTmp = Replace(strTmp,"?","%3F")
    strTmp = Replace(strTmp,":","%3A")
    strTmp = Replace(strTmp,"@","%40")
    strTmp = Replace(strTmp,"=","%3D")
    strTmp = Replace(strTmp,"<","%3C")
    strTmp = Replace(strTmp,">","%3E")
    strTmp = Replace(strTmp,"""","%22")
    strTmp = Replace(strTmp,"{","%7B")
    strTmp = Replace(strTmp,"}","%7D")
    strTmp = Replace(strTmp,"|","%7C")
    strTmp = Replace(strTmp,"\\","%5C")
    strTmp = Replace(strTmp,"^","%5E")
    strTmp = Replace(strTmp,"~","%7E")
    strTmp = Replace(strTmp,"[","%5B")
    strTmp = Replace(strTmp,"]","%5D")
    strTmp = Replace(strTmp,"`","%60")
    EncPunc = strTmp
    End Function

    **********************************************************************
    Sent via Fuzzy Software @ http://www.fuzzysoftware.com/
    Comprehensive, categorised, searchable collection of links to ASP & ASP.NET resources...
    , Oct 13, 2004
    #5
  6. Danny

    Evertjan. Guest

    wrote on 13 okt 2004 in
    microsoft.public.inetserver.asp.general:

    > Function EncPunc(checkPunc)
    > strTmp = checkPunc
    > strTmp = Replace(strTmp,"'","%39")
    > strTmp = Replace(strTmp,"%","%25")
    > strTmp = Replace(strTmp,"&","%26")
    > strTmp = Replace(strTmp,"#","%23")
    >


    teststr = "%&#;/?:mad:=<>""{}|\^~[]`"

    Function EncPunc(x)
    EncPunc = ""
    for i=1 to len(x)
    t = mid(x,i,1)
    if instr(teststr,t)>0 then t = "%" & asc(t)
    EncPunc = EncPunc & t
    next
    End Function

    ==================================

    > strTmp = Replace(strTmp,"\\","%5C")


    you are mistaken, this is not javascript, but vbscript

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress,
    but let us keep the discussions in the newsgroup)
    Evertjan., Oct 13, 2004
    #6
  7. Danny

    regx Guest

    Yes, there is a much easier wa to do HTML entity, unfortunatly as far as I
    can tell regexps in VB are severely impared!!!
    The easy way to do this is to match the char with a regular expresion and
    then get the numeric value for the char.
    In VB it would look something like
    dim reg1 as RegExp
    set reg1= new RegExp
    reg1.global = true
    reg1.pattern="([^a-zA-Z0-9\s])"
    text=reg1.replace(text,"&" & Asc("$1") & ";"

    Unfortunatley in VB you can not pass submatches to a funtion. Submatches
    only work inside the expression.
    If anyone knows how to pass submatches to a function in a VB RegExp please
    let me know.
    For now I just call external Perl scripts from VB

    by the way, the above in Perl looks like this
    $text=~s/([^a-zA-Z0-9\s])/"&".ord($1).";"/ge;

    Aside from being brief, it works!!!
    And if we want to check to make sure entities like $ do not get encoded we
    can pass to a function that handles this
    $text=~s/([^a-zA-Z0-9\s])/encodeHTMLentities($1)/ge;


    "Bob Roby" wrote:

    > I use a function for that. There might a slicker way to do it, but this is what I came up with. You can replace each puncuation mark with just an empty string "" instead of the encoding.
    >
    > Function EncPunc(checkPunc)
    > strTmp = checkPunc
    > strTmp = Replace(strTmp,"'","%39")
    > strTmp = Replace(strTmp,"%","%25")
    > strTmp = Replace(strTmp,"&","%26")
    > strTmp = Replace(strTmp,"#","%23")
    > strTmp = Replace(strTmp,";","%3B")
    > strTmp = Replace(strTmp,"/","%2F")
    > strTmp = Replace(strTmp,"?","%3F")
    > strTmp = Replace(strTmp,":","%3A")
    > strTmp = Replace(strTmp,"@","%40")
    > strTmp = Replace(strTmp,"=","%3D")
    > strTmp = Replace(strTmp,"<","%3C")
    > strTmp = Replace(strTmp,">","%3E")
    > strTmp = Replace(strTmp,"""","%22")
    > strTmp = Replace(strTmp,"{","%7B")
    > strTmp = Replace(strTmp,"}","%7D")
    > strTmp = Replace(strTmp,"|","%7C")
    > strTmp = Replace(strTmp,"\\","%5C")
    > strTmp = Replace(strTmp,"^","%5E")
    > strTmp = Replace(strTmp,"~","%7E")
    > strTmp = Replace(strTmp,"[","%5B")
    > strTmp = Replace(strTmp,"]","%5D")
    > strTmp = Replace(strTmp,"`","%60")
    > EncPunc = strTmp
    > End Function
    >
    > **********************************************************************
    > Sent via Fuzzy Software @ http://www.fuzzysoftware.com/
    > Comprehensive, categorised, searchable collection of links to ASP & ASP.NET resources...
    >


    "Bob Roby" wrote:

    > I use a function for that. There might a slicker way to do it, but this is what I came up with. You can replace each puncuation mark with just an empty string "" instead of the encoding.
    >
    > Function EncPunc(checkPunc)
    > strTmp = checkPunc
    > strTmp = Replace(strTmp,"'","%39")
    > strTmp = Replace(strTmp,"%","%25")
    > strTmp = Replace(strTmp,"&","%26")
    > strTmp = Replace(strTmp,"#","%23")
    > strTmp = Replace(strTmp,";","%3B")
    > strTmp = Replace(strTmp,"/","%2F")
    > strTmp = Replace(strTmp,"?","%3F")
    > strTmp = Replace(strTmp,":","%3A")
    > strTmp = Replace(strTmp,"@","%40")
    > strTmp = Replace(strTmp,"=","%3D")
    > strTmp = Replace(strTmp,"<","%3C")
    > strTmp = Replace(strTmp,">","%3E")
    > strTmp = Replace(strTmp,"""","%22")
    > strTmp = Replace(strTmp,"{","%7B")
    > strTmp = Replace(strTmp,"}","%7D")
    > strTmp = Replace(strTmp,"|","%7C")
    > strTmp = Replace(strTmp,"\\","%5C")
    > strTmp = Replace(strTmp,"^","%5E")
    > strTmp = Replace(strTmp,"~","%7E")
    > strTmp = Replace(strTmp,"[","%5B")
    > strTmp = Replace(strTmp,"]","%5D")
    > strTmp = Replace(strTmp,"`","%60")
    > EncPunc = strTmp
    > End Function
    >
    > **********************************************************************
    > Sent via Fuzzy Software @ http://www.fuzzysoftware.com/
    > Comprehensive, categorised, searchable collection of links to ASP & ASP.NET resources...
    >
    regx, Dec 5, 2004
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    421
  2. VSK
    Replies:
    2
    Views:
    2,269
  3. lucky
    Replies:
    0
    Views:
    322
    lucky
    Feb 9, 2005
  4. J. VerSchave

    regular expression search replace

    J. VerSchave, Feb 28, 2004, in forum: Java
    Replies:
    2
    Views:
    404
    Alan Moore
    Feb 29, 2004
  5. Jimmy
    Replies:
    13
    Views:
    423
    Arne Vajhøj
    Jul 25, 2012
Loading...

Share This Page