How do I write a simple regular expression pattern matching function in C or C++?

后端 未结 9 934
我寻月下人不归
我寻月下人不归 2021-02-01 06:58

This is a question in my paper test today, the function signature is

int is_match(char* pattern,char* string)

The pattern is limited to only A

9条回答
  •  北恋
    北恋 (楼主)
    2021-02-01 07:47

    Didn't test this, actually code it, or debug it, but this might get you a start...

    for each character in the pattern
      if pattern character after the current one is *
        // enter * state
        while current character from target == current pattern char, and not at end
          get next character from target
        skip a char from the pattern
      else if pattern character after the current one is ?
        // enter ? state
        if current character from target == current pattern char
          get next char from target
        skip a char from the pattern
      else
        // enter character state
        if current character from target == current pattern character
          get next character from target
        else
          return false
    return true
    

提交回复
热议问题