Solved

Full-fledged "Not" clause in Regular Expressions

Posted on 2008-06-14
6
559 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
[X]
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
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…

734 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