Regular expression matching wrong answer?


  • 0
    K

    Input: "a", "ab*"
    Output: false
    Expected: true

    Why this input has expected "true" answer? I think "ab*" equals to "ab", "abb", "abbb.......". So the output should be false. Can anyone explain why the expected answer is "true".

    Thanks.


  • 3

    '*' Matches zero or more of the preceding element.


  • 0
    K

    In this case, the "preceding element" is "b". So your input does not answer my question. Thanks.


  • 0

    Huh? How does that not answer your question? Yes, the preceding element is "b". So it also matches when there are zero "b", i.e., when the string is just "a". I don't see why you don't see that.


  • 0

    Ah, I guess I know now. You think there must be an "a", then there must be a "b", and then there must be zero or more "b", right? That's not how regular expressions work. The "*" kind of modifies the "b". It's not separate. You need to read "b*" as a single thing meaning zero or more "b".


  • 0
    K

    Good explanation. Thanks for clarifying!


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.