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 ?
Check if the name does not match [a-zA-Z]*controller or [a-zA-Z]*map.
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.
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.
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.
but using both
public*.class[a-zA-Z]*(?<!controller|map)$
public*.class*.(?<!controller)$
there isnt any match case!!!
@ 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
finally I did it in this way
public.*class.*[^(controller|map|spec)]$
it worked