Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 06-03-2012, 06:46 PM
tinfanide tinfanide is offline VBScript.RegExp: exclude a particular word Windows 7 64bit VBScript.RegExp: exclude a particular word Office 2010 32bit
Expert
VBScript.RegExp: exclude a particular word
 
Join Date: Aug 2011
Posts: 312
tinfanide is on a distinguished road
Default VBScript.RegExp: exclude a particular word

This is not just an Excel VBA question, anyway, I'd post it here for help:

Code:
Dim str6 As String
str6 = "cat and dog"

With CreateObject("VBScript.RegExp")
    
    .Global = True
    .IgnoreCase = True
    .Pattern = "\b[^cat]{3}\b"
    For Each oMatch In .Execute(str6)
        Debug.Print oMatch
    Next oMatch

End With

''' output
''' dog
I want to have any words except "cat" but the above codes also exclude the word "and". How can I do a pattern that excludes a particular word instead of



Code:
.pattern = "[^cat]"
which just shows me
Quote:
(space)
n
d
(space)
d
o
g
in the console.
Reply With Quote
  #2  
Old 06-05-2012, 03:05 PM
Colin Legg's Avatar
Colin Legg Colin Legg is offline VBScript.RegExp: exclude a particular word Windows 7 32bit VBScript.RegExp: exclude a particular word Office 2010 32bit
Expert
 
Join Date: Jan 2011
Location: UK
Posts: 369
Colin Legg will become famous soon enough
Default

Hi,

Here's one pattern (a negative lookahead) which will exclude dog:
Code:
.Pattern = "(?!dog\b)\b\w+"
__________________
Colin

RAD Excel Blog
Reply With Quote
  #3  
Old 06-07-2012, 06:52 AM
tinfanide tinfanide is offline VBScript.RegExp: exclude a particular word Windows 7 64bit VBScript.RegExp: exclude a particular word Office 2010 32bit
Expert
VBScript.RegExp: exclude a particular word
 
Join Date: Aug 2011
Posts: 312
tinfanide is on a distinguished road
Default

Quote:
Originally Posted by Colin Legg View Post
Hi,

Here's one pattern (a negative lookahead) which will exclude dog:
Code:
.Pattern = "(?!dog\b)\b\w+"
A lovely one. I missed the lookahead part when reading online tutorials of regexp.
But just wanna ask why
\b after dog is needed? To state the pattern ends by "dog" before the word boundary "\b"? And the \b outside before \w is used for what?
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
.doc Suddenly Changed to .vbe (VBScript Encoded Script File) emes Word 2 04-24-2012 05:07 PM
VBScript.RegExp: exclude a particular word WORD: Rtf and search-replace (regexp/fonts) seteshpl Word 1 09-06-2011 01:35 AM
Edit spell check dic to exclude words? franklekens Word 1 07-03-2010 09:57 AM
VbScript to Print a .pdf DaveB Excel 2 03-07-2009 12:36 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 02:52 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft