gpt4 book ai didi

c# - 如何选择首选(但特定)正则表达式匹配而不是默认(但通用)正则表达式匹配?

转载 作者:太空宇宙 更新时间:2023-11-03 10:41:56 25 4
gpt4 key购买 nike

我正在 try catch 文本 block 的各个部分,包括它是否有开始或结束引号,然后是文本 block 本身,不包括那些引号。当我只有一个结束引号、句号和结束引号或只有一个结束引号时,正则表达式模式工作正常。

        string test = @"""This has a begin quote, period and end quote.""" + Environment.NewLine +
@"""This has a begin quote and period." + Environment.NewLine +
@"""This has a begin quote and end quote""" + Environment.NewLine +
@"""This has a begin quote only" + Environment.NewLine;

string pattern = @"^\s*(?<BeginQuote>"")?" +
@"(?<BodyPattern>.+((\.(?=""?\s*$))|(?=""\s*$)))" +
@"(?<EndQuote>""(?=\s*$))?";

Regex rx = new Regex(pattern, RegexOptions.Multiline);

MatchCollection matches = rx.Matches(test);

foreach (Match m in matches)
{
GroupCollection groups = m.Groups;
Console.WriteLine("Beginning Quotation Mark: {0}", groups["BeginQuote"].Success);
Console.WriteLine("BodyPattern: {0}", groups["BodyPattern"]);
Console.WriteLine("Ending Quotation Mark: {0}", groups["EndQuote"].Success);
}

这是输出:
开始引号:True
BodyPattern:这有一个开始引号、句点和结束引号。
结束引号:True

开始引号:True
BodyPattern:这有一个开始引号和句点。
结束引号:False

开始引号:True
BodyPattern:这有一个开始引号和结束引号
结束引号:True

问题是当我尝试为既没有结束引号也没有句号的情况提供匹配时。我尝试了多种变体来捕捉字符串的结尾。这总是有效的,但最终也会捕获任何结束引号。如果其他测试不起作用,我如何将此选项设置为“后备”选项?

这是我在 Regex 模式中尝试的一种变体:

        string pattern = @"^\s*(?<BeginQuote>"")?" +
@"(?<BodyPattern>.+((\.(?=""?\s*$))|(?=""\s*$)|($)))" +
@"(?<EndQuote>""(?=\s*$))?";

但是,此模式始终默认为字符串替代的结尾:

开始引号:True
BodyPattern:这有一个开始引号、句点和结束引号。”
结束引号:False

开始引号:True
BodyPattern:这有一个开始引号和句点。
结束引号:False

开始引号:True
BodyPattern:这有一个开始引号和结束引号"
结束引号:False

开始引号:True
BodyPattern:这只有一个开始引号
结束引号:False

我还尝试将替代字符串的结尾作为首选(相同的输出;并尝试使该表达式“惰性”(但我使用“??”进行的几次尝试产生了相同的输出)。我也尝试过具有相同输出的各种备选方案分组(虽然可能不是所有可能性)。

最佳答案

+ 量词是贪婪的,请改用 +?。这应该可以解决问题。

string test = @"""This has a begin quote, period and end quote.""" + "\n" +
@"""This has a begin quote and period." + "\n" +
@"""This has a begin quote and end quote""" + "\n" +
@"""This has a begin quote only";

Regex rx = new Regex(@"(?m)^\s*(?<BeginQuote>"")?(?<BodyPattern>.+?(?:\.|(?=""|$)))(?<EndQuote>"")?");

foreach (Match m in rx.Matches(test)) {
Console.WriteLine("Beginning Quotation Mark: {0}", m.Groups["BeginQuote"].Success);
Console.WriteLine("BodyPattern: {0}", m.Groups["BodyPattern"]);
Console.WriteLine("Ending Quotation Mark: {0}", m.Groups["EndQuote"].Success);
Console.WriteLine("--------------------------");
}

输出

Beginning Quotation Mark: True
BodyPattern: This has a begin quote, period and end quote.
Ending Quotation Mark: True
--------------------------
Beginning Quotation Mark: True
BodyPattern: This has a begin quote and period.
Ending Quotation Mark: False
--------------------------
Beginning Quotation Mark: True
BodyPattern: This has a begin quote and end quote
Ending Quotation Mark: True
--------------------------
Beginning Quotation Mark: True
BodyPattern: This has a begin quote only
Ending Quotation Mark: False
--------------------------

关于c# - 如何选择首选(但特定)正则表达式匹配而不是默认(但通用)正则表达式匹配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25022498/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com