我正在尝试在我的正则表达式中使用[[:>:]]
但是在其他字符类中不接受它们,例如[[:digit:]]
或[[:word:]]
是。fn main() {
let arr: [u8; 10] = [
1, 1, 1,
2, 2, 2,
3, 3, 3, 0
];
let mut results: [u8; 10] = [0; 10];
let corrections: [u8; 10] = [
1, 1, 1,
1, 1, 1,
1, 1, 1, 0
];
let group_ranges = vec![
0..3,
3..6,
6..10
];
for range in group_ranges {
let group_sum: u8 = arr[&range].iter().sum();
for (idx, el) in arr[&range].iter().enumerate() {
results[&range][idx] = el * group_sum * corrections[&range][idx];
}
}
println!("{:?}", results);
// => [3, 3, 3, 12, 12, 12, 27, 27, 27, 0]
}
。出了什么问题?
答案 0 :(得分:3)
您可以改用\b(?<=d)
或\b(?=d)
。在任何情况下,PCRE引擎都会在开始匹配之前将[[:<:]]
转换为\b(?=\w)
并将[[:>:]]
转换为\b(?<=\w)
。
答案 1 :(得分:3)
这是一个错误,因为这些结构(起始字边界,[[:<:]]
和结束[[:>:]]
字边界)are supported by the PCRE library itself:
COMPATIBILITY FEATURE FOR WORD BOUNDARIES In the POSIX.2 compliant library that was included in 4.4BSD Unix, the ugly syntax [[:<:]] and [[:>:]] is used for matching "start of word" and "end of word". PCRE treats these items as follows: [[:<:]] is converted to \b(?=\w) [[:>:]] is converted to \b(?<=\w) Only these exact character sequences are recognized. A sequence such as [a[:<:]b] provokes error for an unrecognized POSIX class name. This support is not compatible with Perl. It is provided to help migrations from other environments, and is best not used in any new patterns. Note that \b matches at the start and the end of a word (see "Simple asser- tions" above), and in a Perl-style pattern the preceding or following character normally shows which is wanted, without the need for the assertions that are used above in order to give exactly the POSIX be- haviour.
在PHP代码中使用时,它可以工作:
if (preg_match_all('/[[:<:]]home[[:>:]]/', 'homeless and home', $m))
{
print_r($m[0]);
}
找到Array ( [0] => home)
。请参阅online PHP demo。
因此,regex101.com开发团队决定(或忘记)包含对这些成对字词边界的支持。
在regex101.com ,而是使用所有4个regex101.com正则表达式引擎支持的\b
字边界(包括起始和结束边界):PCRE,JS,Python和Go。
这些单词边界主要由POSIX类引擎支持,例如,请参阅此PostgreSQL regex demo。 [[:<:]]HR[[:>:]]
正则表达式在Head of HR
中找到匹配项,但在<A HREF="some.html
和CHROME
中找不到匹配项。
支持[[:<:]]
和[[:>:]]
字边界的其他正则表达式引擎是基础R(gsub
,没有perl=TRUE
参数,例如)和MySQL。
在Tcl正则表达式中,\m
(起始字边界)[[:<:]]
和\M
结尾字边界([[:>:]]
)。