user3634329 user3634329 - 1 year ago 57
C Question

What would the regex be

Here's my question I was given: Write a regular expression to recognize strings with that have any number of a's, b's and c's in the order of abc. However, any number of d's may be among the a's, b's and c's.

Positive Examples:

dddaddbcdd

dddd

Negative examples:

dabcddadbdd because 2nd sequence starts but does not finish.

ddcdd because c without leading ab

ddaddbddcaddbcdd because 2 abc sequences

Here's what I've tried:

[^abc]+(a|b|c)*[^abc]

Answer Source

If I understood your question well, the idea is that a must be before b and b before c if any of them are present. Additionally there might be some d in whatever place.

If that's the case then you can use a regex like this:

^d*(?:a+d*b+d*c+d*)?$

Working demo