Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
261 views
in Technique[技术] by (71.8m points)

c# - Regex: Find the lines that contain X but not Y

[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

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

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...