C检查字符串看起来是否同样



嗨,我有字符串,看起来像" ab_dc-05:d5ef6:aef_ "。我想检查另一个字符串是否看起来像这样(在开始时具有0到x空间,在末端和之间的0到x空间中只有字母数值值和":","," - "," _"。什么函数我应该使用吗?顺便说一句,我找到了regex.h库,但我可能不能包括那个,因为我必须在Windows上使用C99。

谢谢

这就是我的方式,类似的事情应该起作用,并且可能比使用re:

更容易
bool matchPattern(const char *s)
{
  // Zero or more spaces at the start.
  while(*s == ' ')
    ++s;
  const char * const os = s;
  while(isalnum((unsigned int) *s) || *s == ':' || *s == '-' || *s == '_')
    ++s;
  // If middle part was empty, fail.
  if(s == os)
    return false;
  // Zero or more spaces at the end.
  while(*s == ' ')
    ++s;
  // The string must end here, or we fail.
  return *s == '';
}

上面尚未测试,但至少应该足以作为灵感。

相关内容

最新更新