Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

[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 (.|
|
).*?]

And instead of the first dot in the (.| | ) 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 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
259 views
Welcome To Ask or Share your Answers For Others

1 Answer

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 ,?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...