-1
[Hi my name is Tom Baker.]
[Hi my name is Jim 
Knopf.]
[Hi my name is Ben Hubbard.]

I need a regex for C# / .Net that matches "[Hi my name is {anything but "Ben"}{anything can follow}]"

I looked up all the examples that explain how to exclude a word but they all then DON'T match the regex. I want the exact opposite.

If the other rules of the regex apply but word X is not contained that that's the match.

Additional problem: This regex needs to be a multiline regex.

What I've got so far is this:

(?m)\[Hi my name is (.|\r|\n).*?\]

And instead of the first dot in the (.|\r|\n) I need an expression that basically says "if this section does not contain 'Ben' then that's a match".

EDIT

Sorry, I need to adapt my request in two ways, information I only realized I ommitted when I saw the given answers.

1) When I wrote

I need a regex for C# / .Net that matches "[Hi my name is {anything but "Ben"}{anything can follow}]"

I actually meant

I need a regex for C# / .Net that matches "[Hi my name is {anything can be here}{anything but "Ben" can be here}{anything can be here}]"

2) The Regex itself must contain the rule that it is a multiline regex, that's why the \r\n and/or the (?m) are in my first attempt, for complicated reasons I can't use Multiline option when construction the Regex instance.

Try to envision my problem with another example: Imagine you were trying to find all classes in your C# code that don't implement IDisposable. Anywhere after the class name and the colon comes a number of interfaces and one of them may or may not be IDisposable. The class declaration only ends when the openening { of the actual implementation comes

public class Test : BaseTest, IDisposable, ICloneable { //no match

public class Test : BaseTest
    , ICloneable
    , IDisposable
{                                                       //no match

public class Test : BaseTest
    , ICloneable
{                                                       //match

Something like that is what I'm trying to accomplish.

Dee J. Doena
  • 1,631
  • 3
  • 16
  • 26

2 Answers2

2

try:

\[Hi,? my name is (?!Ben)[^\]]+\]
  1. Match [Hi my name is with or without a comma after Hi
  2. Match anything but Ben
  3. Keep grabbing till a ]

and you would use it like:

string str = @"[Hi my name is Tom Baker.]
               [Hi my name is Jim 
               Knopf.]
               [Hi, my name is Ben Hubbard.]";

var pattern = @"\[Hi,? my name is (?!Ben)[^\]]+\]";
var result = Regex.Matches(str, pattern, RegexOptions.Multiline);

//result contains [Hi my name is Tom Baker.] and [Hi my name is Jim Knopf.]

If you don't want matches that contain the comma, you can exclude the ,?

Jonesopolis
  • 25,034
  • 12
  • 68
  • 112
1

This would match all lines with a name not beginning with "Ben" (so Ben Hubbard will not match but Hubbard Ben will):

(?m)\[Hi my name is ((?!Ben)|\r|\n).*?\]

If you want to match any name not equaling Ben (so Ben won't match, but Ben Hubbard will), you must use this regex:

(?m)\[Hi my name is ((?!Ben).*|Ben.+|\r|\n)\]

If you want Ben not to be contained at all (not even John McBenson will match), the following regex will do the job:

(?m)\[Hi my name is (((?!Ben).)*|\r|\n)\]
Œlrim
  • 535
  • 2
  • 13