我想知道是否有任何简单的选项可以让字符串等于格式字符串。
例如我希望这种格式 .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/