Results 1 to 9 of 9

Thread: [VBA]: Regular Expression Pattern: Replace & ignore line feeds

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2006
    Location
    Sydney, Australia
    Posts
    42

    [VBA]: Regular Expression Pattern: Replace & ignore line feeds

    Hi All,

    I have using VBA in Microsoft Access to replace special characters in a text string. Can you provide assistance in changing the pattern so line feeds are ignored?

    Pattern: Need to replace all the characters, not in the pattern (enclosed by []) but I don't want to remove the line breaks.

    I don't have much experience with regular expressions but any assistance would be appreciated.

    HTML Code:
    Sub TestReg()
    
        Dim strPattern As String
        Dim strReplace As String:
        Dim regEx As Variant
        Dim GCID As String
        
        strPattern = "[^a-zA-Z0-9 ,/\|().-]" 'The regex pattern to find special characters
            
        strReplace = ""
        Set regEx = CreateObject("vbscript.regexp")
        
        ' ^: Any other than in the []
        ' []: All charactors in brackets
        '
        
        GCID = "Text a ┬ãN-/A %  x" & vbNewLine & "ABC ã"
        
        ' Configure the regex object
        With regEx
            .Global = True
            .MultiLine = True
            .IgnoreCase = False
            .Pattern = strPattern
        End With
    
        ' Perform the regex replacement
        GCID = regEx.Replace(GCID, strReplace)
    
        Debug.Print GCID
    
    ' Result: Text a N-/A   xABC 
    
    End Sub
    
    

  2. #2

  3. #3

    Thread Starter
    Member
    Join Date
    Feb 2006
    Location
    Sydney, Australia
    Posts
    42

    Re: [VBA]: Regular Expression Pattern: Replace & ignore line feeds

    Quote Originally Posted by wqweto View Post
    Try adding \r\n inside [ ] to skip new lines (and carriage returns)
    Excellent, that worked perfectly.

    I have the following text which looks like it has white space

    GCID = "aaa bbb" which contains white space (?asc(" ") >> 160), but I can't seem to find the pattern to ignore, do you have any ideas?

    Thanks,
    Adam.

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [VBA]: Regular Expression Pattern: Replace & ignore line feeds

    perhaps, if this is an isolated case, you could replace chr(160) with chr(32) before the regex
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5

    Thread Starter
    Member
    Join Date
    Feb 2006
    Location
    Sydney, Australia
    Posts
    42

    Re: [VBA]: Regular Expression Pattern: Replace & ignore line feeds

    Quote Originally Posted by westconn1 View Post
    perhaps, if this is an isolated case, you could replace chr(160) with chr(32) before the regex
    Thanks, manually pasted the white space and got it to work, will try this for other special charters I find.

    Thanks,
    Adam.

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [VBA]: Regular Expression Pattern: Replace & ignore line feeds

    if there are multiple different characters you could create a loop to replace each one
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  7. #7

    Thread Starter
    Member
    Join Date
    Feb 2006
    Location
    Sydney, Australia
    Posts
    42

    Re: [VBA]: Regular Expression Pattern: Replace & ignore line feeds

    Quote Originally Posted by westconn1 View Post
    if there are multiple different characters you could create a loop to replace each one
    Thanks, I have looked at that also, that will be my last resort.

  8. #8
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,120

    Re: [VBA]: Regular Expression Pattern: Replace & ignore line feeds

    You can use non-printable symbols in the character classes too (the symbols between [ and ]) like this

    strPattern = "[^a-zA-Z0-9 ,/\|().\r\n" & Chr$(160) & "-]"


    Just keep the dash (-) as the last character because otherwise it's interpreted as range specifier (like in a-z you use for all small Latin letters) or escape it with \-

    You can even search for Chr$(0) inside VBScript's regexp character class i.e. specifying non-printable symbols with Chr function is common practice.

    cheers,
    </wqw>

  9. #9

    Thread Starter
    Member
    Join Date
    Feb 2006
    Location
    Sydney, Australia
    Posts
    42

    Re: [VBA]: Regular Expression Pattern: Replace & ignore line feeds

    Quote Originally Posted by wqweto View Post
    You can use non-printable symbols in the character classes too (the symbols between [ and ]) like this

    strPattern = "[^a-zA-Z0-9 ,/\|().\r\n" & Chr$(160) & "-]"


    Just keep the dash (-) as the last character because otherwise it's interpreted as range specifier (like in a-z you use for all small Latin letters) or escape it with \-

    You can even search for Chr$(0) inside VBScript's regexp character class i.e. specifying non-printable symbols with Chr function is common practice.

    cheers,
    </wqw>
    Brilliant, I will check it out, much appreciated.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width