c |比较字符串格式

标签 c ansi-c

我想知道是否有任何简单的选项可以让字符串等于格式字符串。 例如我希望这种格式 .mat[something][something] 等于使用 strcmp.mat[r1][r2].mat[4][5]

是否有任何选项可以使用某种正则表达式?或类似 strcmp(.mat[%s][%s], .mat[r3][r5])?

顺便说一句,我正在使用 ansi-c 谢谢

最佳答案

使用最接近正则表达式的东西,scanf 扫描集,这可行,但它非常难看:

char row[20], col[20], bracket[2], ignored;
if (sscanf(input, ".mat[%19[^]]][%19[^]]%1[]]%c", row, col, bracket, &ignored) == 3) {
    // row and col contain the corresponding specifications...    
    // bracket contains "]"
    // %c failed to convert because if the end of string
    ....
}

这是 ".mat[r1][r2]" 的分解转换规范:

".mat["    // matches .mat[
"%19[^]]"  // matches r1   count=1
"]["       // matches ][
"%19[^]]"  // matches r2   count=2
"%1[]]"    // matches ]    count=3
"%c"       // should not match anything because of the end of string

关于c |比较字符串格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45572861/

相关文章:

c - 字符串数组初始化

c - 有没有办法找出指针指向什么类型的结构?

c - 使用 sun rpc 从服务器发送结构数组到客户端

c++ - 将 float 转换为字符串

c - 将字符串的 ASCII 值写入文件 C

c - volatile 指针指向非 volatile 数据

c - 这是什么类型的函数(带代码的指针函数)?

c - 排序数组在 ANSI C 中不起作用

c - 如何使用 sscanf 获取名称并按照我想要的方式打印它们

c - pthread 的问题