簡體   English   中英

在C或C ++中使用GNU regex函數

[英]Working with GNU regex functions in C or C++

誰能給我完整的示例程序,說明如何在gcc C或C ++中使用GNU regex函數( http://docs.freebsd.org/info/regex/regex.info.GNU_Regex_Functions.html ),以及re_pattern_bufferre_compile_fastmap嗎?

例如,翻譯這個小的Python程序:

import re
unlucky = re.compile('1\d*?3')

nums = ("13", "31", "777", "10003")

for n in nums:
    if unlucky.search(n) is None:
        print "lucky"
    else:
        print "unlucky"

謝謝!

好吧,在深入研究代碼之前,我應該提到您可能想使用更高級別的庫。 您確實說過C ++,所以這使您可以使用Boost.Regex等。 即使您希望繼續使用C,也有更好的選擇。 我發現POSIX函數簡潔一些,更不用說更便攜了。

// Tell GNU to define the non-standard APIs
#define _GNU_SOURCE 
// This is actually the same header used for the POSIX API.
// Except then you obviously don't need _GNU_SOURCE
#include <regex.h> 
#include <stdio.h>
#include <string.h>

int main()
{
    struct re_pattern_buffer pat_buff; // Put a re_pattern_buffer on the stack
    // The next 4 fields must be set.  

    // If non-zero, applies a translation function to characters before 
    // attempting match (http://www.delorie.com/gnu/docs/regex/regex_51.html)
    pat_buff.translate = 0; 
    // If non-zero, optimization technique.  Don't know details.
    // See http://www.delorie.com/gnu/docs/regex/regex_45.html
    pat_buff.fastmap = 0;
    // Next two must be set to 0 to request library allocate memory
    pat_buff.buffer = 0;
    pat_buff.allocated = 0;
    char pat_str[] = "1[^3]*3";
    // This is a global (!) used to set the regex type (note POSIX APIs don't use global for this)
    re_syntax_options = RE_SYNTAX_EGREP; 
    // Compile the pattern into our buffer
    re_compile_pattern(pat_str, sizeof(pat_str) - 1, &pat_buff); 
    char* nums[] = {"13", "31", "777", "10003"}; // Array of char-strings
    for(int i = 0; i < sizeof(nums) / sizeof(char*); i++)
    {
        int match_ret;
        // Returns number of characters matches (may be 0, but if so there's still a match)
        if((match_ret = re_match(&pat_buff, nums[i], strlen(nums[i]), 0, NULL)) >= 0) 
        {
            printf("unlucky\n");
        }
        else if(match_ret == -1) // No match
        {
            printf("lucky\n");
        }
        // Anything else (though docs say -2) is internal library error
        else 
    {
            perror("re_match");
        }
    }
    regfree(&pat_buff);
}

編輯:我增加了對必填字段和regfree的更多解釋。 我之前很幸運/很倒霉,這解釋了部分差異。 另一部分是,我不認為這里提供的任何正則表達式語法都支持惰性運算符(*?)。 在這種情況下,有一個簡單的解決方法,使用"1[^3]*3"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM