views:

139

answers:

4

hi can any body tell me how to use regex for negation of string? I wanna find all line that start with public class and then any thing except first,second and finally any thing else.

for example in the result i expect to see public class base but not public class myfirst:base can any body help me please??

+2  A: 

Use a negative lookahead:

public\s+class\s+(?!first|second).+
SLaks
it didnt return any thing
adra
@adra: Take out the `=`.
SLaks
A: 

Here is something that should work for you

/public\sclass\s(?:[^fs\s]+|(?!first|second)\S)+(?=\s|$)/

The second look a head could be changed to a $(end of line) or another anchor that works for your particular use case, like maybe a '{'

Edit: Try changing the last part to:

(?=\s|$)
tsgrasser
it didnt work niether
adra
+1  A: 

Both the other two answers come close, but probably fail for different reasons.

public\s+class\s+(?:(?!first|second).)+

Note how there is a (non-capturing) group around the negative lookahead, to ensure it applies to more than just the first position.

And that group is less restrictive - since . excludes newline, it's using that instead of \S, and the $ is not necessary - this will exclude the specified words and match others.

No slashes wrapping the expression since those aren't required in everything and may confuse people that have only encountered string-based regex use.

If this still fails, post the exact content that is wrongly matched or missed, and what language/ide you are using.

Update:
Turns out you're using Visual Studio, which has it's own special regex implementation, for some unfathomable reason. So, you'll be wanting to try this instead:

public:b+class:b+~(first|second)+$

I have no way of testing that - if it doesn't work, try dropping the $, but otherwise you'll have to find a VS user. Or better still, the VS engineer(s) responsible for this stupid non-standard regex.

Peter Boughton
unforttunatly but it didnt return any thing niether
adra
i'm using c#.the result is:Find all "public\s+class\s+(?:(?!first|second).)+", Subfolders, Find Results 1, "Entire Solution" Matching lines: 0 Matching files: 0 Total files searched: 13
adra
How many of those 13 files match when you search for `public\s+class\s+.+` ?
Peter Boughton
Hmmm, it sounds like you're using Visual Studio IDE search, (*not* C# code). The regex implementation in VS is completely screwed up, so that's why these are all failing. I'll update my answer with a VS solution too.
Peter Boughton
@ Peter Boughton it didnt return any thing
adra
+1  A: 

If Peter is correct and you're using Visual Studio's Find feature, this should work:

^:b*public:b+class:b+~(first|second):i.*$

:b matches a space or tab
~(...) is how VS does a negative lookahead
:i matches a C/C++ identifier

The rest is standard regex syntax:
^ for beginning of line
$ for end of line
. for any character
* for zero or more
+ for one or more
| for alternation

Alan Moore
For completeness, you should add "`|` for alternation" to your second key.
Peter Boughton
Done. You know, there's actually some good ideas in the VS regex flavor. It's too bad it's so thoroughly incompatible with every other Perl-derived flavor.
Alan Moore
Yeah, the stuff like `:i` does seem a good idea, but it should really have been implemented in a compatible way, or marked more clearly (e.g. in the drop-down have "Regular expressions (.NET)" and "Regular expressions (VS-style)" or whatever).
Peter Boughton
its driving me crazy, non of them work in vs's find feature.
adra
i was wrong,thank u alan,it worked. i'm so happy
adra