I have an input string like "hello, dflk 1234 12345678, wod-=0, 87654321" and I would like to get a list of all “words”, which do not correspond to regular expression pattern "\d{8}" (eight digits in a row).
I studied java.util.regex api doc, however I was unable to find a way to put together negation of regular expression “\d{8}”. Here’s the way I would like to use it:
String input = "hello, dflk 1234 12345678, wod-=0, 87654321";
List<String> hitList = new ArrayList<>();
Pattern p = Pattern.compile(...?...); //<- how to define the regex pattern?
Matcher m = p.matcher(input);
while(m.find()) {
hitList.add(m.group());
}
I’d like to have all of these in my hitList (based on the input string above):
"hello," "dflk" "1234" "," "wod-=0,"
Can you suggest a way to define that regex pattern?
One way (that’s probably the easiest) is to filter the resulting list of matches.
Another would be a negative lookahead and a few more lookarounds:
Pretty is different, I think.
Quick PowerShell test: