如何用双引号内的引号识别令牌
How to recognize token with quote inside of double quotes
是否有任何库方法或普遍认可的方法来识别双引号内有引号的标记,同时仍将没有双引号的单引号识别为标记?
例如,字符串:"Bill's Pot" 'Roast'
应生成标记:
Bill's Pot
Roast
没有库函数可以专门执行此操作,但是有一些库函数可以帮助您自己执行此操作,例如 strchr
以获取指向特定字符中您选择的字符的指针字符串(如果存在)和 isspace
来检测未加引号的字符串的 space 个字符,尽管 isspace
也取决于语言环境。如果您只想删除 "C" 语言环境中定义的 space 个字符,只需使用 strspn
和第二个参数 " \f\n\r\t\v"
而不是调用 [=12 的循环=] 重复(注意该字符串开头的 space 字符)。
这是一种使用附加规则解析示例字符串的方法,允许 C 样式反斜杠转义以允许嵌入引号。请注意,它仅检测由空格字符分隔的字符串的开头和结尾,这意味着它实际上不会替换转义引号或执行任何其他操作:
char str[] = "\"Bill's Pot\" 'Roast'";
char *start;
char *end;
start = str;
while (*start) {
// Skip leading spaces.
while (isspace(*start))
++start;
// Double-quoted string with backslash escapes.
if (*start == '"') {
end = strchr(++start, '"');
while (end != NULL && *end == '"' && end[-1] == '\')
end = strchr(++end, '"');
if (end == NULL || *end == '[=10=]') {
fprintf(stderr, "Unterminated double-quoted string -- %s\n", --start);
break;
}
}
// Single-quoted string with backslash escapes.
else if (*start == '\'') {
end = strchr(++start, '\'');
while (end != NULL && *end == '\'' && end[-1] == '\')
end = strchr(++end, '\'');
if (end == NULL || *end == '[=10=]') {
fprintf(stderr, "Unterminated single-quoted string -- %s\n", --start);
break;
}
}
// Unquoted (space-delimited) string.
else if (*start != '[=10=]') {
end = start + 1;
while (*end != '[=10=]' && !isspace(*end))
++end;
}
// Empty string.
else
end = start;
printf("%.*s\n", end - start, start);
// Quotes must be skipped before continuing parsing.
if (*end == '\'' || *end == '"')
++end;
// Get ready to start the next round of parsing.
start = end;
}
您也可以避免使用字符串库函数,而只进行自己的字符串解析。这允许您以灵活的方式处理 Bill"'s Pot"
形式的字符串。应该是一串Bill's Pot
还是两串Bill
's Pot
?存在 alternative methods to escape quotation marks and even other ways to delimit strings in addition to single and double quotation marks 以及引用规则 la POSIX sh
允许您在字符串中嵌入换行符,这意味着开始引号和结束引号位于不同的两行,C禁止。在后一种情况下,仅 C 字符串函数是不够的,因为您需要一个状态变量来指示您在单引号或双引号字符串中。这应该让您了解@JonathanLeffler 在他的评论中的意思;有这么多不同的引用规则!希望我提供的代码能让您了解如何做您想做的事。
是否有任何库方法或普遍认可的方法来识别双引号内有引号的标记,同时仍将没有双引号的单引号识别为标记?
例如,字符串:"Bill's Pot" 'Roast'
应生成标记:
Bill's Pot
Roast
没有库函数可以专门执行此操作,但是有一些库函数可以帮助您自己执行此操作,例如 strchr
以获取指向特定字符中您选择的字符的指针字符串(如果存在)和 isspace
来检测未加引号的字符串的 space 个字符,尽管 isspace
也取决于语言环境。如果您只想删除 "C" 语言环境中定义的 space 个字符,只需使用 strspn
和第二个参数 " \f\n\r\t\v"
而不是调用 [=12 的循环=] 重复(注意该字符串开头的 space 字符)。
这是一种使用附加规则解析示例字符串的方法,允许 C 样式反斜杠转义以允许嵌入引号。请注意,它仅检测由空格字符分隔的字符串的开头和结尾,这意味着它实际上不会替换转义引号或执行任何其他操作:
char str[] = "\"Bill's Pot\" 'Roast'";
char *start;
char *end;
start = str;
while (*start) {
// Skip leading spaces.
while (isspace(*start))
++start;
// Double-quoted string with backslash escapes.
if (*start == '"') {
end = strchr(++start, '"');
while (end != NULL && *end == '"' && end[-1] == '\')
end = strchr(++end, '"');
if (end == NULL || *end == '[=10=]') {
fprintf(stderr, "Unterminated double-quoted string -- %s\n", --start);
break;
}
}
// Single-quoted string with backslash escapes.
else if (*start == '\'') {
end = strchr(++start, '\'');
while (end != NULL && *end == '\'' && end[-1] == '\')
end = strchr(++end, '\'');
if (end == NULL || *end == '[=10=]') {
fprintf(stderr, "Unterminated single-quoted string -- %s\n", --start);
break;
}
}
// Unquoted (space-delimited) string.
else if (*start != '[=10=]') {
end = start + 1;
while (*end != '[=10=]' && !isspace(*end))
++end;
}
// Empty string.
else
end = start;
printf("%.*s\n", end - start, start);
// Quotes must be skipped before continuing parsing.
if (*end == '\'' || *end == '"')
++end;
// Get ready to start the next round of parsing.
start = end;
}
您也可以避免使用字符串库函数,而只进行自己的字符串解析。这允许您以灵活的方式处理 Bill"'s Pot"
形式的字符串。应该是一串Bill's Pot
还是两串Bill
's Pot
?存在 alternative methods to escape quotation marks and even other ways to delimit strings in addition to single and double quotation marks 以及引用规则 la POSIX sh
允许您在字符串中嵌入换行符,这意味着开始引号和结束引号位于不同的两行,C禁止。在后一种情况下,仅 C 字符串函数是不够的,因为您需要一个状态变量来指示您在单引号或双引号字符串中。这应该让您了解@JonathanLeffler 在他的评论中的意思;有这么多不同的引用规则!希望我提供的代码能让您了解如何做您想做的事。