-2

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]
duncan
  • 1,149
  • 8
  • 14

1 Answers1

0

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

Federico Piazza
  • 27,409
  • 11
  • 74
  • 107