0

Cの文字列に置換する必要があります。ここの回答の1つで推奨されていました純粋なCで正規表現文字列置換を行う方法は? PCRS ライブラリを使用します。ここから PCRS をダウンロードしましたftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/Contrib/ですが、使い方がわかりません。以下は私のコードです(別のSE投稿から取得)

            const char *error;
            int   erroffset;
            pcre *re;
            int   rc;
            int   i;
            int   ovector[100];

            char *regex = "From:([^@]+).*";
            char str[]  = "From:regular.expressions@example.com\r\n";
            char stringToBeSubstituted[] = "gmail.com";

            re = pcre_compile (regex,          /* the pattern */
                               PCRE_MULTILINE,
                               &error,         /* for error message */
                               &erroffset,     /* for error offset */
                               0);             /* use default character tables */
            if (!re)
            {
                printf("pcre_compile failed (offset: %d), %s\n", erroffset, error);
                return -1;
            }

            unsigned int offset = 0;
            unsigned int len = strlen(str);
            while (offset < len && (rc = pcre_exec(re, 0, str, len, offset, 0, ovector, sizeof(ovector))) >= 0)
            {
                for(int i = 0; i < rc; ++i)
                {
                    printf("%2d: %.*s\n", i, ovector[2*i+1] - ovector[2*i], str + ovector[2*i]);
                }
                offset = ovector[1];
            }

「pcre_compile」と「pcre_exec」ではなく、PCRS からどの関数を使用する必要がありますか?

ありがとう。

4

1 に答える 1

0

INSTALLファイルの指示に従ってください。

PCRS をビルドするには、pcre 3.0 以降と gcc が必要です。

インストールは簡単です: ./configure && make && make install --enable-debug でデバッグモードを有効にできます。

簡単なデモ アプリケーション (pcrsed) が含まれています。

PCRS は、man ページに記載されている次の機能を提供しますpcrs.3

  • pcrs_compile
  • pcrs_compile_command
  • pcrs_execute
  • pcrs_execute_list
  • pcrs_free_job
  • pcrs_free_joblist
  • pcrs_strerror

man ページのオンラインバージョンを次に示します。これらの関数を使用するには、ヘッダー ファイルpcrs.hをインクルードし、リンカー フラグを使用して PCRS ライブラリに対してプログラムをリンクします-lpcrs

于 2013-09-18T17:52:11.523 に答える