Solved

Full-fledged "Not" clause in Regular Expressions

Posted on 2008-06-14
6
548 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

744 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

11 Experts available now in Live!

Get 1:1 Help Now