I need a regular expression to match any string other than none. I tried using regular exp ="^[^none]$", But it does not work.
-
Do you mean "other than 'none'"? If so why not match 'none' and negate the result? – Dmitry B. Dec 06 '11 at 17:20
-
Do you only want to fail if your string is "none"? – FailedDev Dec 06 '11 at 20:55
4 Answers
You can use the regular expression (?!^none$).*
. See this question for details: Regex inverse matching on specific string?
The reason "^[^none]$"
doesn't work is that you are actually matching all strings except the strings "n", "o", or "e".
Of course, it would be easier to just use String.equals
like so: !"none".equals(testString)
.

- 1
- 1

- 31,931
- 18
- 65
- 77
-
The regular expression "^(?!none).+$" is not accepting the String nonetheless. The regular expression should not accept none. It should accept all other words. How can I modify the expression? – user679526 Dec 06 '11 at 20:21
-
@user679526 I got it now. `^(?!none).+$` matches any word that doesn't start with "none". I edited my answer with the regex `(?!^none$).*` which should match every string except "none". – Jack Edmonds Dec 07 '11 at 14:24
If you are matching a String
against a specific word in Java you should use equals()
. In this case you want to invert the match so your logic becomes:
if(!theString.equals("none")) {
// do stuff here
}
Much less resource hungry, and much more intuitive.
If you need to match a String which contains the word "none", you are probably looking for something like:
if(theString.matches("\\bnone\\b")) {
/* matches theString if the substring "none" is enclosed between
* “word boundaries”, so it will not match for example: "nonetheless"
*/
}
Or if you can be fairly certain that “word boundaries” mean a specific delimiter you can still evade regular expressions by using the indexOf()
method:
int i = theString.indexOf("none");
if(i > -1) {
if(i > 0) {
// check theString.charAt(i - 1) to see if it is a word boundary
// e.g.: whitespace
}
// the 4 is because of the fact that "none" is 4 characters long.
if((theString.length() - i - 4) > 0) {
// check theString.charAt(i + 4) to see if it is a word boundary
// e.g.: whitespace
}
}
else {
// not found.
}

- 11,576
- 2
- 31
- 26
Actually this is the regex to match all words except "word":
Pattern regex = Pattern.compile("\\b(?!word\\b)\\w+\\b");
Matcher regexMatcher = regex.matcher(subjectString);
while (regexMatcher.find()) {
// matched text: regexMatcher.group()
// match start: regexMatcher.start()
// match end: regexMatcher.end()
}
You must use word boundaries so that "word" is not contained in other words.
Explanation:
"
\b # Assert position at a word boundary
(?! # Assert that it is impossible to match the regex below starting at this position (negative lookahead)
Lorem # Match the characters “Lorem” literally
\b # Assert position at a word boundary
)
\w # Match a single character that is a “word character” (letters, digits, etc.)
+ # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
\b # Assert position at a word boundary
"

- 26,680
- 9
- 53
- 73
This is the regex you are looking for:
Pattern p = Pattern.compile("^(?!none$).*$");
Matcher m = p.matcher("your string");
System.out.println(s + ": " + (m.matches() ? "Match" : "NO Match"));
Having that said, if you are not forced to use a regex that matches everything but "none", the more simple, fast, clear, and easy to write and understand is this:
Pattern p = Pattern.compile("^none$");
Then, you just exclude the matches.
Matcher m = p.matcher("your string");
System.out.println(s + ": " + (m.matches() ? "NO Match" : "Match"));

- 1,922
- 15
- 26