用于匹配整个单词的 Glib 正则表达式?
Glib regex for matching whole word?
要匹配整个单词,正则表达式 \bword\b
就足够了。然而下面的代码总是 returns 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 模式 returns 与测试引擎中提供的字符串的一个匹配项。但是在这个程序中它 returns 0 匹配。代码有错吗? Glib 中的哪个正则表达式可用于匹配整个单词?
看来你也必须转义反斜杠:
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
您可以使用 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;
}
要匹配整个单词,正则表达式 \bword\b
就足够了。然而下面的代码总是 returns 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 模式 returns 与测试引擎中提供的字符串的一个匹配项。但是在这个程序中它 returns 0 匹配。代码有错吗? Glib 中的哪个正则表达式可用于匹配整个单词?
看来你也必须转义反斜杠:
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
您可以使用 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;
}