I need a java regex pattern to validate input String: the input can containt 3 or more letters, followed by 7 or more digits. The sum of the characters should be between 10 and 14.
I wrote a pattern, and tested working, I realized this with 2 sections: 1 positive lookahead that checks for characters format (3 or more letters followed by 7 or more numbers) 2 positive lookahead checks for input string character length in mass
My pattern: (?=^[A-Z]{3,}[0-9]{7,}$)(?=^[A-Z0-9]{10,14}$)
When I use in java8 with Matcher.matches(), it does not match instead if I use matcher.find(), it gives me true.
I tried this pattern: (?=^[A-Z]{3,}[0-9]{7,}$)(?=^[A-Z0-9]{10,14}$) with Matcher.matches() and was expecting to give me true, but give me false.
If I try this pattern with matcher.find(), it gives me true, but I also have other patterns in use, and that don`t have start and end sign, so find() function gives true for that pattern (gives wrong result) if the input string contains other characters too (so I would not use find because other patterns if not neccessarry).
input should work: ROM1234567 ROMM1234567 ROM123456789
input should not work: RO1234567 RO123456 ROM123456 ROM123456789012
Matcher.matches()checks if full string matches provided pattern. But you pattern doesn't actually matches anything: lookaheads (and lookarounds in general) do not consume input.You can either use pattern that actually matches string. Like this:
or
Demo of the first example here. Notice, how it matches full line, instead of empty string in the beginning, like your attempt did it.
Or use
matcher.find()since it looks for substring and perfectly happy with pattern that matches empty string in the beginning of the input.