使用 scanf() 将标识符读入 C 程序

发布于 2024-12-19 11:55:40 字数 351 浏览 3 评论 0原文

我需要我的 C 程序能够使用 C 中的 scanf() 方法读取标识符。

在这种情况下,标识符是一个字母或 _ 字符,后跟一个或多个字母数字字符(包括 _ 字符)。

正则表达式将是

    [a-ZA-Z_][a-zA-Z0-9_]*

这些是正确标识符的示例:

    _identifier1
    variable21

这些是错误标识符的示例

    12var
    %foobar

有谁知道如何在 C 中使用 scanf() 来完成此操作?

I need my C program to be able to read in an identifier using the scanf() method in C.

An identifier in this case is a letter or a _ character followed by one or more alphanumeric characters including the _ character.

The regular expression would be

    [a-ZA-Z_][a-zA-Z0-9_]*

These are examples of correct identifiers:

    _identifier1
    variable21

These are examples of incorrect identifiers

    12var
    %foobar

Does anybody know how this would be done using scanf() in C?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小瓶盖 2024-12-26 11:55:40

scanf() 不支持正则表达式。标准 C 库中根本不支持正则表达式。您必须读取字符串,然后“手动”解析它。

例如:

#include <stdio.h>
#include <ctype.h>

int isIdentifier(const char* s)
{
  const char* p = s;

  if (!(*p == '_' || isalpha(*p)))
  {
    return 0;
  }

  for (p++; *p != '\0'; p++)
  {
    if (!(*p == '_' || isalnum(*p)))
    {
      return 0;
    }
  }

  return 1;
}

int main(void)
{
  const char* const testData[] =
  {
    "a",
    "a_",
    "_a",
    "3",
    "3a",
    "3_",
    "_3"
  };
  int i;

  for (i = 0; i < sizeof(testData) / sizeof(testData[0]); i++)
  {
    printf("\"%s\" is %san identifier\n",
           testData[i],
           isIdentifier(testData[i]) ? "" : "not ");
  }

  return 0;
}

输出:

"a" is an identifier
"a_" is an identifier
"_a" is an identifier
"3" is not an identifier
"3a" is not an identifier
"3_" is not an identifier
"_3" is an identifier

scanf() doesn't support regular expressions. There's no regular expression support at all in the standard C library. You'll have to read a string and then parse it "manually".

For example:

#include <stdio.h>
#include <ctype.h>

int isIdentifier(const char* s)
{
  const char* p = s;

  if (!(*p == '_' || isalpha(*p)))
  {
    return 0;
  }

  for (p++; *p != '\0'; p++)
  {
    if (!(*p == '_' || isalnum(*p)))
    {
      return 0;
    }
  }

  return 1;
}

int main(void)
{
  const char* const testData[] =
  {
    "a",
    "a_",
    "_a",
    "3",
    "3a",
    "3_",
    "_3"
  };
  int i;

  for (i = 0; i < sizeof(testData) / sizeof(testData[0]); i++)
  {
    printf("\"%s\" is %san identifier\n",
           testData[i],
           isIdentifier(testData[i]) ? "" : "not ");
  }

  return 0;
}

Output:

"a" is an identifier
"a_" is an identifier
"_a" is an identifier
"3" is not an identifier
"3a" is not an identifier
"3_" is not an identifier
"_3" is an identifier
何以畏孤独 2024-12-26 11:55:40

scanf 的格式说明符有点有限。它们不能用于识别您的标识符的模式。我相信您只能对读取的字符串执行自定义验证,例如:(

int in_range(char ch, char begin, char end)
{
    return ch >= begin && ch <= end;
}

int valid_start_char(char ch)
{
    return in_range(ch, 'a', 'z') ||
        in_range(ch, 'A', 'Z') ||
        ('_' == ch);
}

int valid_char(char ch)
{
    return valid_start_char(ch) || in_range(ch, '0', '9');
}

// ..

char buff[255];
int i, len = 0, valid = 0;
scanf("%s", buff);

len = strlen(buff);

if(len > 0)
    valid = valid_start_char(buff[0]);

for(i = 1 ; i < len ; ++i)
    valid = valid && valid_char(buff[i]);

if(valid)
    printf("Valid Identifier\n");
else
    printf("Invalid Identifier\n");

我还没有测试过这个,但它应该说明这个想法)

scanf's format specifiers are kind of limited. They cannot be used to identify the pattern of your identifier. I believe you can only perform custom validation on the read string, something like:

int in_range(char ch, char begin, char end)
{
    return ch >= begin && ch <= end;
}

int valid_start_char(char ch)
{
    return in_range(ch, 'a', 'z') ||
        in_range(ch, 'A', 'Z') ||
        ('_' == ch);
}

int valid_char(char ch)
{
    return valid_start_char(ch) || in_range(ch, '0', '9');
}

// ..

char buff[255];
int i, len = 0, valid = 0;
scanf("%s", buff);

len = strlen(buff);

if(len > 0)
    valid = valid_start_char(buff[0]);

for(i = 1 ; i < len ; ++i)
    valid = valid && valid_char(buff[i]);

if(valid)
    printf("Valid Identifier\n");
else
    printf("Invalid Identifier\n");

(I haven't tested this but it should illustrate the idea)

生生漫 2024-12-26 11:55:40

如果您习惯使用正则表达式,为什么不直接使用正则表达式库呢?如果您使用的是 POSIX 兼容操作系统,则应该有一个正则表达式库。

If you're comfortable with using regex why not just use a regex library? If you're using a POSIX compliant operating system there should be a regex library.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文