Reputation: 23
Is it possible in regex to combine two validations? Whenever a series of 8 digit numbers(12345678) are given in a statement along with space(1234 5678) Regex should throw an error. Along with validation for alphabets &other special character .
Eg: 1234 apartment ,#23building state 234 456 (valid one)
1234 apartment ,#23building state 1234 5678 overhill (invalid one because of series/continuous of 8 digits)
677 flat, @(23) floor, up state 56789123 state UK ( invalid because of continuous 8 digits)
Upvotes: 0
Views: 218
Reputation: 22817
(?:\d *){8}
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
final String regex = "(?:\\d *){8}";
final String[] strings = {
"1234 offshore address1234 Some locations",
"1234 5678 offshore some location"
};
final Pattern pattern = Pattern.compile(regex);
for (String s: strings) {
Matcher matcher = pattern.matcher(s);
if(!matcher.find()) {
System.out.println(s);
}
}
}
}
1234 offshore address1234 Some locations
1234 5678 offshore some location
Below shows strings that don't match.
1234 offshore address1234 Some locations
(?:\d *){8}
matches a digit followed by any number of spaces exactly 8 times
Upvotes: 1