编辑代码

#include <stdio.h>
#include <regex.h>
int main() {
    // 歌词字符串
    char *lyrics = "[ti:传奇]\n"
                   "[ar:王菲]\n"
                   "\n"
                   "[00:03.50]传奇\n"
                   "[00:19.10]作词:刘兵 作曲:李健\n"
                   "[00:20.60]演唱:王菲\n"
                   "[00:26.60]\n"
                   "[04:40.75][02:39.90][00:36.25]只是因为在人群中多看了你一眼\n"
                   "[04:49.00]\n"
                   "[02:47.44][00:43.69]再也没能忘掉你容颜\n"
                   "[02:54.83][00:51.24]梦想着偶然能有一天再相见\n"
                   "[03:02.32][00:58.75]从此我开始孤单思念\n"
                   "[03:08.15][01:04.30]\n"
                   "[03:09.35][01:05.50]想你时你在天边\n"
                   "[03:16.90][01:13.13]想你时你在眼前\n"
                   "[03:24.42][01:20.92]想你时你在脑海\n"
                   "[03:31.85][01:28.44]想你时你在心田\n"
                   "[03:38.67][01:35.05]\n"
                   "[04:09.96][03:39.87][01:36.25]宁愿相信我们前世有约\n"
                   "[04:16.37][03:46.38][01:42.47]今生的爱情故事 不会再改变\n"
                   "[04:24.82][03:54.83][01:51.18]宁愿用这一生等你发现\n"
                   "[04:31.38][04:01.40][01:57.43]我一直在你身旁 从未走远。";

    // 正则表达式
    char *regex_lyric = "(?<=\\])[\u4e00-\u9fa5]+.*"; // 匹配歌词
    char *regex_time = "(?<=\\[)[\\d]+:[\\d]+\\.[\\d]+"; // 匹配歌词时间
    char *regex_title = "(?<=ti:)[\u4e00-\u9fa5]+"; // 匹配歌名
    char *regex_singer = "(?<=ar:)[\u4e00-\u9fa5]+"; // 匹配歌手

    // 编译正则表达式
    regex_t reg_lyric, reg_time, reg_title, reg_singer;
    regcomp(&reg_lyric, regex_lyric, REG_EXTENDED);
    regcomp(&reg_time, regex_time, REG_EXTENDED);
    regcomp(&reg_title, regex_title, REG_EXTENDED);
    regcomp(&reg_singer, regex_singer, REG_EXTENDED);

    // 匹配正则表达式
    regmatch_t pmatch[1]; // 存储匹配结果的位置信息
    int offset = 0; // 记录匹配的偏移量
    int nmatch = 1; // 匹配的子表达式个数

    // 匹配歌名
    if (regexec(&reg_title, lyrics, nmatch, pmatch, 0) == 0) {
        // 提取歌名
        char title[pmatch[0].rm_eo - pmatch[0].rm_so + 1];
        for (int i = pmatch[0].rm_so; i < pmatch[0].rm_eo; i++) {
            title[i - pmatch[0].rm_so] = lyrics[i];
        }
        title[pmatch[0].rm_eo - pmatch[0].rm_so] = '\0';
        // 输出歌名
        printf("歌名:%s\n", title);
    }

    // 匹配歌手
    if (regexec(&reg_singer, lyrics, nmatch, pmatch, 0) == 0) {
        // 提取歌手
        char singer[pmatch[0].rm_eo - pmatch[0].rm_so + 1];
        for (int i = pmatch[0].rm_so; i < pmatch[0].rm_eo; i++) {
            singer[i - pmatch[0].rm_so] = lyrics[i];
        }
        singer[pmatch[0].rm_eo - pmatch[0].rm_so] = '\0';
        // 输出歌手
        printf("歌手:%s\n", singer);
    }

    // 匹配歌词和时间
    printf("歌词:\n");
    while (regexec(&reg_lyric, lyrics + offset, nmatch, pmatch, 0) == 0) {
        // 提取歌词
        char lyric[pmatch[0].rm_eo - pmatch[0].rm_so + 1];
        for (int i = pmatch[0].rm_so; i < pmatch[0].rm_eo; i++) {
            lyric[i - pmatch[0].rm_so] = lyrics[offset + i];
        }
        lyric[pmatch[0].rm_eo - pmatch[0].rm_so] = '\0';
        // 输出歌词
        printf("%s\n", lyric);
        // 更新偏移量
        offset += pmatch[0].rm_eo;
        // 匹配时间
        while (regexec(&reg_time, lyrics + offset, nmatch, pmatch, 0) == 0) {
            // 提取时间
            char time[pmatch[0].rm_eo - pmatch[0].rm_so + 1];
            for (int i = pmatch[0].rm_so; i < pmatch[0].rm_eo; i++) {
                time[i - pmatch[0].rm_so] = lyrics[offset + i];
            }
            time[pmatch[0].rm_eo - pmatch[0].rm_so] = '\0';
            // 输出时间
            printf("%s\n", time);
            // 更新偏移量
            offset += pmatch[0].rm_eo;
        }
    }

    // 释放正则表达式
    regfree(&reg_lyric);
    regfree(&reg_time);
    regfree(&reg_title);
    regfree(&reg_singer);

    return 0;
}