[Solved] Regular expression for accept only 1 occurrence of “space” and “single quote” character among letters [closed]


 quote | space ||
-------+-------++-----
   0   |   0   || ^[a-z]*$
   0   |   1   || ^[a-z]* [a-z]*$
   1   |   0   || ^[a-z]*'[a-z]*$
   1   |   1   || ^[a-z]* [a-z]*'[a-z]*$ or ^[a-z]*'[a-z]* [a-z]*$

Eg.

final Pattern regex = Pattern
        .compile("^([a-z]*|[a-z]* [a-z]*|[a-z]*'[a-z]*|[a-z]* [a-z]*'[a-z]*|[a-z]*'[a-z]* [a-z]*)$",
                CASE_INSENSITIVE);

for(String x: asList("", "'", " ", "' '", " a ", "p%q", "aB cd'Ef", "'dF gh"))
    System.out.printf("%s <= \"%s\"%n", regex.matcher(x).matches(), x);

With output:

true <= ""
true <= "'"
true <= " "
false <= "' '"
false <= " a "
false <= "p%q"
true <= "aB cd'Ef"
true <= "'dF gh"

1

solved Regular expression for accept only 1 occurrence of “space” and “single quote” character among letters [closed]