You didn't specify if you want an expression for matching individual strings (Pattern#matches(String, CharSequence)
or String#matches(String)
) or if you want to find multiple matches in one string (Matcher#find()
).
Matching a Single String
The following branch is taken if input
is Test or 123, but not if it's 123-123:
if (input.matches("(?s)(?!\\d+-\\d+).*")) {
// ...
}
Although, if your code makes the actual matches
call, you might as well write:
if (!input.matches("\\d+-\\d+")) {
// ...
}
Finding Matches in a String
If you want to find multiple matches in one string, much depends on how you want to tokenize. Here's example code that lists all whitespace-separated sequences that are not of the form \d+-\d+
:
Pattern pattern = Pattern.compile("(?<=^|\\s)(?!\\d+-\\d+)\\S+(?=$|\\s)");
Matcher matcher = pattern.matcher(input);
while (matcher.find()) {
System.out.println(matcher.group());
}
For an input of "Test 123 Test 123-123 Test", the output would be:
Test
123
Test
Test
If you want to tokenize differently, you will have to adjust the boundary matching. Java's regular expressions have a word boundary matcher (\b
), but 123-123 wouldn't be considered a word, because it contains a hyphen, which is not a regex word character.