tags:

views:

110

answers:

7

how can I write regular expression that dose not contain some string at the end. in my project,all classes that their names dont end with some string such as "controller" and "map" should inherit from a base class. how can I do this using regular expression ?

A: 

Check if the name does not match [a-zA-Z]*controller or [a-zA-Z]*map.

fastcodejava
This is too restrictive - who knows whether other characters that ASCII letters aren't permitted in the string?
Tim Pietzcker
`/(?<=\btha)t/n/g` :)
Tim Pietzcker
using bothpublic*.class[a-zA-Z]*(?<!controller|map)$public*.class*.(?<!controller)$there isnt any match case!!!
adrakadabra
+4  A: 

Do a search for all filenames matching this:

(?<!controller|map|anythingelse)$

(Remove the |anythingelse if no other keywords, or append other keywords similarly.)

If you can't use negative lookbehinds (the (?<!..) bit), do a search for filenames that do not match this:

(?:controller|map)$

And if that still doesn't work (might not in some IDEs), remove the ?: part and it probably will - that just makes it a non-capturing group, but the difference here is fairly insignificant.

If you're using something where the full string must match, then you can just prefix either of the above with ^.* to do that.


Update:

In response to this:

but using both
public*.class[a-zA-Z]*(?<!controller|map)$
public*.class*.(?<!controller)$
there isnt any match case!!!

Not quite sure what you're attempting with the public/class stuff there, so try this:

public.*class.*(?<!controller|map)$`

The . is a regex char that means "anything except newline", and the * means zero or more times.

If this isn't what you're after, edit the question with more details.

Peter Boughton
the class name can be any thing, just it shouldnt finish with "controller" or "map". for example in the result i expect to see public class class1, but not public class class1controller
adrakadabra
@adracadabra: This is something you should put into your question, not into a comment to an answer. Welcome to the new and different world of StackOverflow :) (OT: What happened to aBracadabra?)
Tim Pietzcker
adrakadabra, what language or tool are you using this regex with?
Peter Boughton
+1  A: 

You could write a regex that contains two groups, one consists of one or more characters before controller or map, the other contains controller or map and is optional.

^(.+)(controller|map)?$

With that you may match your string and if there is a group() method in the regex API you use, if group(2) is empty, the string does not contain controller or map.

FK82
+1  A: 

Depending on your regex implementation, you might be able to use a lookbehind for this task. This would look like

(?<!SomeText)$

This matches any lines NOT having "SomeText" at their end. If you cannot use that, the expression

^(?!.*SomeText$).*$

matches any non-empty lines not ending with "SomeText" as well.

Jens
Your second regex fails when `SomeText` is present *anywhere* in the string and is therefore too restrictive.
Tim Pietzcker
@Tim: Thanks for pointing that out! Here's a corrected version.
Jens
A: 

but using both

public*.class[a-zA-Z]*(?<!controller|map)$ 
public*.class*.(?<!controller)$ 

there isnt any match case!!!

adrakadabra
Whatever you were planning to write, it's not an answer and should be deleted here. Put any additional info into your question (you can always edit your own question). Also, please specify which regex flavor you're using or all else will be guesswork.
Tim Pietzcker
A: 

@ Peter Boughton:the class name can be any thing, just it shouldnt finish with "controller" or "map". for example in the result i expect to see public class class1, but not public class class1controller

adrakadabra
Add comments instead of posting answers as comments please.
FK82
A: 

finally I did it in this way

public.*class.*[^(controller|map|spec)]$

it worked

adrakadabra