正则表达式:最小可能匹配或非贪婪匹配

发布于 2024-08-15 14:20:10 字数 46 浏览 4 评论 0原文

如何告诉 RegEx(.NET 版本)获取最小的有效匹配而不是最大的有效匹配?

How do I tell RegEx (.NET version) to get the smallest valid match instead of the largest?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

那请放手 2024-08-22 14:20:10

对于 .*.+ 等正则表达式,请附加问号(.*?.+?) 匹配尽可能少的字符。要选择性地匹配 (?:blah)? 部分,但除非绝对必要,否则不匹配,请使用类似 (?:blah){0,1}? 的内容。对于重复匹配(使用 {n,}{n,m} 语法)附加问号以尝试匹配尽可能少的内容(例如 {3,}?{5,7}?)。

关于正则表达式量词的文档也可能乐于助人。

For a regular expression like .* or .+, append a question mark (.*? or .+?) to match as few characters as possible. To optionally match a section (?:blah)? but without matching unless absolutely necessary, use something like (?:blah){0,1}?. For a repeating match (either using {n,} or {n,m} syntax) append a question mark to try to match as few as possible (e.g. {3,}? or {5,7}?).

The documentation on regular expression quantifiers may also be helpful.

长途伴 2024-08-22 14:20:10

非贪婪运算符?。就像这样:

.*?

The non-greedy operator, ?. Like so:

.*?
贵在坚持 2024-08-22 14:20:10

非贪婪运算符并不意味着最短的可能匹配;例如,在字符串上

abcabk

...a.+?k 将匹配整个字符串(在本例中),而不是仅匹配最后三个符号。

我实际上想找到最小的可能匹配。

这是 'a' 的最后一个可能的匹配,但仍然允许 k 的所有匹配。

对于单字符起始序列,一种方法是使用如下表达式:

a[^a]+?k

const haystack = 'abcabkbk';
const paternNonGreedy = /a.+?k/;
const paternShortest = /a[^a]+?k/;

const matchesNonGreedy = haystack.match(paternNonGreedy);
const matchesShortest = haystack.match(paternShortest);

console.log('non greedy: ',matchesNonGreedy[0]);
console.log('shortest: ', matchesShortest[0]);

The non-greedy operator does not mean the shortest possible match; for example, on string

abcabk

a.+?k will match the entire string (in this example) instead of only the last three signs.

I'd like to actually find the smallest possible match instead.

That is that last possible match for 'a' to still allow all matches for k.

For single-character start sequences, one way to do that is to make use of an expression like:

a[^a]+?k

const haystack = 'abcabkbk';
const paternNonGreedy = /a.+?k/;
const paternShortest = /a[^a]+?k/;

const matchesNonGreedy = haystack.match(paternNonGreedy);
const matchesShortest = haystack.match(paternShortest);

console.log('non greedy: ',matchesNonGreedy[0]);
console.log('shortest: ', matchesShortest[0]);

半世蒼涼 2024-08-22 14:20:10

负向前瞻在这里会有所帮助

示例:

a...a.....a..b


a.*?b            =>   a...a.....a..b
a(((?!a).)*?)b   =>   a..b

a 和 b 可以更大

start...start......start..end


start.*?end                =>   start...start.....start..end
start(((?!start).)*?)end   =>   start..end

注意:这不会找到字符串中最短的匹配。

a...a.....a..b.a.b


a.*?b            =>   a...a.....a..b
a(((?!a).)*?)b   =>   a..b

这仍然找到 a..b 而不是 ab,因此它不是“最小可能匹配”。我不确定您是否可以使用正则表达式找到尽可能小的匹配项。您可以找到所有匹配项,然后在这些结果中找到最小的。

A negative lookahead would help here

Example:

a...a.....a..b


a.*?b            =>   a...a.....a..b
a(((?!a).)*?)b   =>   a..b

a and b can be larger

start...start......start..end


start.*?end                =>   start...start.....start..end
start(((?!start).)*?)end   =>   start..end

Note: this won't find the shortest match in the string.

a...a.....a..b.a.b


a.*?b            =>   a...a.....a..b
a(((?!a).)*?)b   =>   a..b

This still finds a..b not a.b so it's not "Smallest possible match". I'm not sure you can find smallest possible match with regex. You could find all matches and then in those results find the smallest.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文