-1

I want to skip the string if it contains 'have' after Vehicles word str 1 = "3 Vehicles have already been added", str 2 = "13 Vehicles" Regex I m using = ([0-9]+)[\s]*Vehicles[\s]*[^have]

output needed is "13 Vehicles"(only) but "3 Vehicles" is also matches..that is not right

Thanks in advance

Jenny
  • 17
  • 10

1 Answers1

0

You could use a negative lookahead to assert that have is not directly at the right.

The negated character class [^have] matches a single char which is not any of the listed chars.

[0-9]+\h+Vehicles(?!\h+have\b)

Regex demo

In Java

String regex = "[0-9]+\\h+Vehicles(?!\\h+have\\b)";
The fourth bird
  • 154,723
  • 16
  • 55
  • 70