Search code examples
regexregular-language

Regular expresion problems


So I have to make a regex for the following language that checks (a|b|c)* with the propriety that the string wont end in "abc". So I have made the following regex:

((a|b|c)*((a|b)+|(a|c)+|b(b|c)+))

which is how my teacher taught me however I tested it on https://regex101.com/ and with some more research I have learned that in theory it should be equivalent to the following regex :

([abc]*([ab]+|[ac]+|b[bc]+))

However the website does some funny things with my regex and I dont know how to use it. So what would be the regex for my Language ?


Solution

  • I would advice to use negative lookbehind pattern (https://www.regular-expressions.info/lookaround.html#lookbehind) to check whether string does not end with some sequence.

    ^(?:a|b|c)*(?<!abc)$
    or
    (?:a|b|c)*(?<!abc)
    

    The processor look for text consist of abc and when it find the maximum string it checks, whether there is NOT "abc" at the end. If yes, it throws away last char and try it again and again, until it is possible to continue in following steps.

    see examples https://regex101.com/r/jBIjlu/1 and https://regex101.com/r/jBIjlu/2