Glib正则表达式匹配整个单词?

时间:2018-04-09 00:03:25

标签: regex pcre glib vala

为了匹配整个单词,正则表达式\bword\b应该足够了。然而,以下代码始终返回0匹配

try {
        string pattern = "\bhtml\b";
        Regex wordRegex = new Regex (pattern, RegexCompileFlags.CASELESS, RegexMatchFlags.NOTEMPTY);
        MatchInfo matchInfo;
        string lineOfText = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">";

        wordRegex.match (lineOfText, RegexMatchFlags.NOTEMPTY, out matchInfo);
        stdout.printf ("Match count is: %d\n", matchInfo.get_match_count ());
    } catch (RegexError regexError) {
        stderr.printf ("Regex error: %s\n", regexError.message);
    }

这应该用于测试 \ bhtml \ b 模式返回测试引擎中提供的字符串的一个匹配项。但是在这个程序中它返回0个匹配。代码错了吗? Glib中的正则表达式将用于匹配整个单词吗?

2 个答案:

答案 0 :(得分:1)

看起来你也必须逃避反斜杠:

try {
        string pattern = "\\bhtml\\b";
        Regex wordRegex = new Regex (pattern, RegexCompileFlags.CASELESS, RegexMatchFlags.NOTEMPTY);
        MatchInfo matchInfo;
        string lineOfText = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">";

        wordRegex.match (lineOfText, RegexMatchFlags.NOTEMPTY, out matchInfo);
        stdout.printf ("Match count is: %d\n", matchInfo.get_match_count ());
    } catch (RegexError regexError) {
        stderr.printf ("Regex error: %s\n", regexError.message);
    }

输出:

Match count is: 1

Demo

答案 1 :(得分:1)

您可以使用regular expression literals简化代码:

Regex regex = /\bhtml\b/i;

您不必在正则表达式文字语法中引用反斜杠。 (虽然前斜线会有问题。)

完整示例:

void test_match (string text, Regex regex) {
    MatchInfo match_info;
    if (regex.match (text, RegexMatchFlags.NOTEMPTY, out match_info)) {
        stdout.printf ("Match count is: %d\n", match_info.get_match_count ());
    }
    else {
        stdout.printf ("No match");
    }
}

int main () {
    Regex regex = /\bhtml\b/i;
    test_match ("<!DOCTYPE html PUBLIC>", regex);

    return 0;
}