Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Full-fledged "Not" clause in Regular Expressions

Posted on 2008-06-14
6
Medium Priority
?
570 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 51

Accepted Solution

by:
ahoffmann earned 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

971 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