What regular expression will match a string of letter Us and O so that at most one letter U is adjacent to a letter O?
We wish to match strings of text such as OOOOUUUU and UUUUOOO.
However, the Us and Os should not touch each-other much, as in OUOUUUOOO
| STRING | IS_MATCH |
|---|---|
| empty string | no |
U |
yes |
O |
yes |
UUUU |
yes |
OOOO |
yes |
OU |
yes |
UO, |
yes |
UUUOOOO |
yes |
OOOOUUU |
yes |
OOOUUUUOOUOU |
no |
OUUUOOOUUUO |
no |
OUOUOUOUOUOUOU |
no |
The letters O and U should appear consecutively, at most once.
Suppose that AS is a collection of string literals such that for any string A in set AS, we have that our ideal regex R matches string literal A.
AS = {"U", "O", "OU", "UO", "UOO", "OOU", "UUO", "OOU", ...}
Let A be a string in collection AS.
It follows that if UO is a sub-sequence of string A then OU is NOT a subsequence of string A.
Additionally, if OU is a sub-sequence of string A then UO is NOT a subsequence of string A.
Use an alternation for O's then any U's, or visa versa:
See live demo.