-
May 21st, 2022, 11:22 PM
#1
Thread Starter
Member
[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
-
May 22nd, 2022, 02:27 AM
#2
Re: [VBA]: Regular Expression Pattern: Replace & ignore line feeds
Try adding \r\n inside [ ] to skip new lines (and carriage returns)
-
May 22nd, 2022, 05:16 AM
#3
Thread Starter
Member
Re: [VBA]: Regular Expression Pattern: Replace & ignore line feeds
Originally Posted by wqweto
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.
-
May 23rd, 2022, 04:22 AM
#4
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
-
May 23rd, 2022, 06:34 AM
#5
Thread Starter
Member
Re: [VBA]: Regular Expression Pattern: Replace & ignore line feeds
Originally Posted by westconn1
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.
-
May 24th, 2022, 03:58 AM
#6
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
-
May 24th, 2022, 05:15 AM
#7
Thread Starter
Member
Re: [VBA]: Regular Expression Pattern: Replace & ignore line feeds
Originally Posted by westconn1
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.
-
May 24th, 2022, 05:33 AM
#8
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>
-
May 24th, 2022, 05:55 AM
#9
Thread Starter
Member
Re: [VBA]: Regular Expression Pattern: Replace & ignore line feeds
Originally Posted by wqweto
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|