.net 6 值转换器中的正则表达式问题
我正在尝试学习一些 .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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正则表达式中缺少一些部分,例如,它没有转义大括号
{
和}
,因为大括号在正则表达式中具有特殊含义;它们用作 量词。使用下面的一个。
为了提取大括号之间的数学表达式,它使用 命名捕获组,名称为
mathExpression
。_Expression\{
:从固定文本_Expression{
开始(?
:启动名为mathExpression 的命名捕获组
.+?
:以非贪婪方式获取下一个字符)
:结束指定的捕获组\}
:以以下结尾固定字符}
下面的示例将输出 2 个匹配的
输出
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\{
: start with the fixed text_Expression{
(?<mathExpression>
: start a named capturing group with namemathExpression
.+?
: 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
Output