Regular expression help

The strings to be matched are based on length. String S should be matched iff (|S| mod 3) > (|S| mod 2) where |S| is the length of the string.

I've tried to find wrt LCM but I don't know how to proceed with the solution.

The length that can be accepted is 2, 4, 5, 8, 10, 11, ... How to build this regex.

1

1 Answer

let the strings consist of lowercase alphabets for simplicity, so [a-z] is one character

The accepted lengths are a sequence 2, 4, 5, 8, 10, 11, 14, 16, 17, 20, 22, ... We can group these as {2}, {4,5,8}, {10,11,14}, {16,17,20}, ...

Notice that 4 on the addition of 6 gives 10, 5 on the addition of 6 gives 11, and so on.

To build the regex, consider the special cases first: length of 2, regex would be [a-z]{2} similarly for lengths 4, 5 and 8 regex would be [a-z]{4}, [a-z]{5}, [a-z]{8}

For lengths greater than 8, zero or more, six-character string is appended so the final regex would be

[a-z]{2} + ( [a-z]{4} )( [a-z]{6} )* + ( [a-z]{5} )( [a-z]{6} )* + ( [a-z]{8} )( [a-z]{6} )*
1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like