Search code examples
regexcharacter-encodingsetregular-language

Constructing Strings using Regular Expressions


Assuming you have a a set E = {a,b}, and you have a superset E* consisting of all possible combinations of a, and b in E. How do you construct an expression for a String that has number of a's divisible by 3?


Solution

  • Try

    /^(b*ab*ab*ab*)+$/
    

    Examples:

    abbb => N
    bbb => N
    aaab => Y
    ababbbba => Y
    aaabba => N
    bbbaaaabbbabbbba => Y
     => N