Solved

Full-fledged "Not" clause in Regular Expressions

Posted on 2008-06-14
6
550 Views
Last Modified: 2013-12-26
I am using using regular expressions in VB (reference "Microsoft VBScript Regular Expressions 5.5"), but I include Unix developers in the conversation since Unix developers use Regular Expressions so extensively.  I hope that the answer provided is true for the genre of Regular Expressions and not application specific.
Given my research into regular expressions, I have not found a "Not" clause in Regular expressions that applies for more than single characters.  
For example, say that you want to capture the entire string for people whose names appear right after a left curly bracket ("{"), and you want to capture the text between the two curly brackets, with the exclusion of two people, Bob and Roberta.  How do you do it?  
Inside square brackets, the carot symbol ("^") indicates "Not" for whatever else appears within the brackets; but outside the sqare brackets, the same symbol represents the start of the string.  In the code snippet below, I have used the carot symbol twice: the first time incorrectly (not "Bob" nor "Roberta") and the second time correctly (not a right curly bracket).  
Given that my first use of the carot for "Not" is incorrect, can I legitimatly single out Bob and Roberta for exclusion?
Thanks, ~Peter Ferber


"{(^(Bob|Roberta)[^}]+)}"

Open in new window

0
Comment
Question by:PeterFrb
  • 3
  • 2
6 Comments
 
LVL 27

Expert Comment

by:ddrudik
ID: 21786355
Supply an example of a source text string, and what it should match.
0
 

Author Comment

by:PeterFrb
ID: 21786395
OK:
     "{John Smith is here!}" --> "John Smith is here!"
     "{Doug Kinsey makes hay.}" --> "Doug Kinsey makes hay."
     "{Bob is my guy.}" --> No match.
     "{Roberta did not show up.}" --> No match.
~Peter
0
 
LVL 27

Expert Comment

by:ddrudik
ID: 21786647
You'll have to remove the leading { since vbscript regexes do not support (?<=) constructs:
Raw Match Pattern:

\{(?:(?!\b(?:Bob|Roberta)\b).)*(?=\})
 

$matches Array:

(

    [0] => Array

        (

            [0] => {John Smith is here!

            [1] => {Doug Kinsey makes hay.

        )
 

)

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 51

Accepted Solution

by:
ahoffmann earned 500 total points
ID: 21791697
> For example, say that you want to capture the entire string for people whose names appear right after a left curly bracket ("{"), and you want to capture the text between the two curly brackets, with the exclusion of two people, Bob and Roberta.  How do you do it?  

raw regex (assuming that Bobby is valid):

\{((?!(?:Bob|Roberta)).*)\}

following should also work:

\{((?!Bob)(?!Roberta).*)\}
0
 

Author Comment

by:PeterFrb
ID: 21796646
Thank you for these answers.  I have a bit on my plate today, and I promise to give your answers a try either by end of day or Tuesday morning and, if they work, will assign credit.  Sorry for the delay, and thanks for your patience.
~Peter
0
 

Author Closing Comment

by:PeterFrb
ID: 31486063
Superb answer.  Sorry I was delayed in getting back to this.  I just checked the work against the four items we chose, and it was spot on!  Good work.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now