Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Full-fledged "Not" clause in Regular Expressions

Posted on 2008-06-14
6
557 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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.

807 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