1

I need a regex, that matches anything but the pattern \d+-\d+

The to be ignored pattern is a number with one ore more digits followed by a hyphen then followed by another number with one or more digits.

For example:

Test 123 Test 123-123 Test

only "123-123" should not be matched

Can anyone help me to achieve this?

chribrue
  • 21
  • 2

1 Answers1

1

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.

mistercake
  • 633
  • 5
  • 13
  • 1
    I needed to find this pattern in a string. So the second part of your answer solved my problem. Thank you very much! – chribrue Jun 04 '17 at 21:58