Home > Software engineering >  Regular expression issues in .net 6 value converter
Regular expression issues in .net 6 value converter

Time:03-16

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! :)

CodePudding user response:

There 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
  • Related