PeterFrb
asked on
Full-fledged "Not" clause in Regular Expressions
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
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)[^}]+)}"
Supply an example of a source text string, and what it should match.
ASKER
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
"{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
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.
)
)
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
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
~Peter
ASKER
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.