.net 6 值转换器中的正则表达式问题

发布于 2025-01-14 03:45:37 字数 2031 浏览 2 评论 0原文

我正在尝试学习一些 .net6 和 c#,但我在正则表达式方面遇到了很多困难。更具体地说,是 Windows 中的 Avalonia(如果相关的话)。 我正在尝试制作一个带有 2 个文本框的小应用程序。我在其中一个上写入文本,然后使用值转换器在另一个上“过滤”文本。 我想过滤数学表达式以便稍后尝试解决它们。一些简单的东西,一种编写文本数学并实时获得结果的方式。 几周来我一直在尝试自己计算这个正则表达式,但没有成功。 我想将字符串“_Expression{BLABLA}”替换为“BLABLA”。为了测试我的表达式,我一直在检查 http://regexstorm.net/https://regex101.com/ 根据他们的说法,我的匹配应该是正确的(除非我误解了结果)。但我的小应用程序中的结果对我来说非常奇怪,我最终决定寻求帮助。 这是我的代码:

        private static string? FilterStr(object value)
        {
            if (value is string str)
            {
                string pattern = @"\b_Expression{(.+?)\w*}";
                Regex rgx = new(pattern);
                foreach (Match match in rgx.Matches(str))
                {
                    string aux = "";
                    aux = match.Value;
                    aux = Regex.Replace(aux, @"_Expression{", "");
                    aux = Regex.Replace(aux, @"[\}]", "");
                    str = Regex.Replace(str, match.Value, aux);
                }
                return new string(str);
            }
            return null;
        }

然后一些示例输入的结果是: 输入:

Some text
_Expression{x}
_Expression{1}
_Expression{4}
_Expression{4.5} _Expression{4+4}
_Expression{4-4} _Expression{4*x}
_Expression{x/x}
_Expression{x^4}
_Expression{sin(x)}

输出:

Some text
x
1{1}
1{4}
1{4.5} 1{4+4}
1{4-4} 1{4*x}
1{x/x}
1{x^4}
1{sin(x)}

或 输入:

Some text
_Expression{x}

_Expression{4}
_Expression{4.5} _Expression{4+4}
_Expression{4-4} _Expression{4*x}
_Expression{x/x}
_Expression{x^4}
_Expression{sin(x)}

输出:

Some text
x

_Expression{4}
4.5 _Expression{4+4}
4-4 _Expression{4*x}
x/x
_Expression{x^4}
_Expression{sin(x)}

这种行为让我感到非常困惑。我不明白为什么“(.+?)”对其中一些不起作用,而对另一些却起作用......或者也许我没有正确定义某些内容或者我的替换是错误的?我看不到...

非常感谢您花时间! :)

I am trying to learn some .net6 and c# and I am struggling with regular expressions a lot. More specificaly with Avalonia in Windows if that is relevant.
I am trying to do a small app with 2 textboxes. I write text on one and get the text "filtered" in the other one using a value converter.
I would like to filter math expressions to try to solve them later on. Something simple, kind of a way of writing text math and getting results real time.
I have been trying for several weeks to figure this regular expression on my own with no success whatsoever.
I would like to replace in my string "_Expression{BLABLA}" for "BLABLA". For testing my expressions I have been checking in http://regexstorm.net/ and https://regex101.com/ and according to them my matches should be correct (unless I misunderstood the results). But the results in my little app are extremely odd to me and I finally decided to ask for help.
Here is my code:

        private static string? FilterStr(object value)
        {
            if (value is string str)
            {
                string pattern = @"\b_Expression{(.+?)\w*}";
                Regex rgx = new(pattern);
                foreach (Match match in rgx.Matches(str))
                {
                    string aux = "";
                    aux = match.Value;
                    aux = Regex.Replace(aux, @"_Expression{", "");
                    aux = Regex.Replace(aux, @"[\}]", "");
                    str = Regex.Replace(str, match.Value, aux);
                }
                return new string(str);
            }
            return null;
        }

Then the results for some sample inputs are:
Input:

Some text
_Expression{x}
_Expression{1}
_Expression{4}
_Expression{4.5} _Expression{4+4}
_Expression{4-4} _Expression{4*x}
_Expression{x/x}
_Expression{x^4}
_Expression{sin(x)}

Output:

Some text
x
1{1}
1{4}
1{4.5} 1{4+4}
1{4-4} 1{4*x}
1{x/x}
1{x^4}
1{sin(x)}

or
Input:

Some text
_Expression{x}

_Expression{4}
_Expression{4.5} _Expression{4+4}
_Expression{4-4} _Expression{4*x}
_Expression{x/x}
_Expression{x^4}
_Expression{sin(x)}

Output:

Some text
x

_Expression{4}
4.5 _Expression{4+4}
4-4 _Expression{4*x}
x/x
_Expression{x^4}
_Expression{sin(x)}

It feels very confusing to me this behaviour. I can't see why "(.+?)" does not work with some of them and it does with others... Or maybe I haven't defined something properly or my Replace is wrong? I can't see it...

Thanks a lot for the time! :)

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

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

发布评论

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

评论(1

忱杏 2025-01-21 03:45:37

正则表达式中缺少一些部分,例如,它没有转义大括号 {},因为大括号在正则表达式中具有特殊含义;它们用作 量词

使用下面的一个。
为了提取大括号之间的数学表达式,它使用 命名捕获组,名称为 mathExpression

 _Expression\{(?<mathExpression>.+?)\}
  • _Expression\{ :从固定文本_Expression{ 开始
  • (? :启动名为 mathExpression 的命名捕获组
  • .+? :以非贪婪方式获取下一个字符
  • ) :结束指定的捕获组
  • \} :以以下结尾固定字符}

下面的示例将输出 2 个匹配的

Regex regex = new(@"_Expression\{(?<mathExpression>.+?)\}");
var matches = regex.Matches(@"_Expression{4.5} _Expression{4+4}");
foreach (Match match in matches.Where(o => o.Success))
{
    var mathExpression = match.Groups["mathExpression"];
    Console.WriteLine(mathExpression);
}

输出

4.5
4+4

There are some missing parts in your regular expression, for example it doesn't have the curly braces { and } escaped, since curly braces have a special meaning in a regular expression; they are used as quantifiers.

Use the one below.
For extracting the math expression between the curly braces, it uses a named capturing group with name mathExpression.

 _Expression\{(?<mathExpression>.+?)\}
  • _Expression\{ : start with the fixed text_Expression{
  • (?<mathExpression> : start a named capturing group with name mathExpression
  • .+? : take the next characters in a non greedy way
  • ) : end the named capturing group
  • \} : end with the fixed character }

The below example will output 2 matches

Regex regex = new(@"_Expression\{(?<mathExpression>.+?)\}");
var matches = regex.Matches(@"_Expression{4.5} _Expression{4+4}");
foreach (Match match in matches.Where(o => o.Success))
{
    var mathExpression = match.Groups["mathExpression"];
    Console.WriteLine(mathExpression);
}

Output

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