根据其中为每个值报告的不同 charnum 拆分字符串

Split string based on different charnum reported in it for each value

如何拆分以特定格式定义的一些字符串:

[length relevant char number]name=value[length relevant char number]name=value[length relevant char number]name=value[length relevant char number]name=value

是否可以用正则表达式或简单的 C 代码拆分它?


例如

输入:

0010name=mario0013surname=rossi0006age=180014address=street

输出:

name=mario surname=rossi age=18 address=street

这种解析不需要正则表达式。只需获取 4 位整数字符串,将其转换为 int,然后从字符串中获取更多字符。重复直到结束:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void split_string(const char *s) {
  while (*s) {
    char lenstr[5] = { 0 };
    memcpy(lenstr, s, 4); // Extract the length
    s += 4; // And advance past those digits
    int len = strtol(lenstr, NULL, 10);
    printf("%.*s%c", len, s, s[len] ? ' ' : '\n'); // print len characters
    s += len; // and advance past them to the next element
  }
}

int main(void) {
  const char *input =
    "0010name=mario0013surname=rossi0006age=180014address=street";
  split_string(input);
  return 0;
}

编译并 运行,打印出 name=mario surname=rossi age=18 address=street


从文件读取输入的版本:

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void split_file(FILE *in) {
  while (1) {
    char lenstr[5] = {0};
    size_t n;
    if ((n = fread(lenstr, 1, 4, in)) != 4) {
      if ((n == 0 || (n == 1 && lenstr[0] == '\n')) &&
          feof(in)) { // Done with input
        break;
      }
      fprintf(stderr, "Failed to read length: %s\n",
              feof(in) ? "Unexpected end of file" : strerror(errno));
      exit(EXIT_FAILURE);
    }
    char *eol;
    size_t len = strtoul(lenstr, &eol, 10);
    if (*eol) { // Should be null
      fputs("Invalid format!\n", stderr);
      exit(EXIT_FAILURE);
    }
    char *field = malloc(len + 1);
    if (fread(field, 1, len, in) != len) {
      fprintf(stderr, "Failed to read field: %s\n",
              feof(in) ? "Unexpected end of file" : strerror(errno));
      exit(EXIT_FAILURE);
    }
    field[len] = 0;
    printf("%s ", field);
    free(field);
  }
  putchar('\n');
}

int main(int argc, char **argv) {
  if (argc != 2) {
    fprintf(stderr, "Usage: %s input-filename\n", argv[0]);
    return EXIT_FAILURE;
  }

  FILE *in = fopen(argv[1], "r");
  if (!in) {
    fprintf(stderr, "Unable to open '%s': %s\n", argv[1], strerror(errno));
    return EXIT_FAILURE;
  }

  split_file(in);
  fclose(in);

  return 0;
}