0

I have this ruby expression as below

  (a|bc)(d?|e)*

when i use rubular to test out possible strings that fit this expression, I have some strings that I dont understand why they dont fit

the strings are "ade", it matches "ad" but does not match the "e". Anyone can help?

2 Answers 2

1

The second part of the regular expression you entered (d?|e)* is the problem. Putting the ? on the d says, match d 0 or 1 times. When you run through the string ade, the regex matches a, then d, then d 0 times... If you instead changed it to (a|bc)(d|e)*, it would match ade, and seem to have the semantics that you're looking for.

Sign up to request clarification or add additional context in comments.

1 Comment

just wondering, so any part of the string that contains an e will not match the regular expression?
1

(d?)* is a non-greedy match and e* will be "short circuited" by logic or. It will match as few as possible.

I don't know why you put a question mark there. Just use

(a|bc)(d|e)*

Will be fine.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.