-6

my goal is the evaluate the following using .net core and c#.

"XYZ = XYZ"

My goal is the use the following string to evaluate if the string will return true or false. In this example the result should be true whilst in this example:

"XYdddZ = XYZ"

`Which should return false instead.

essentially it would look similar to:

if ("XYZ = XYZ"){

}

EDIT:

the string may be dynamic and contain something like "XYZ = XYZ and XYZ != XYaZ" Therefor splitting may be hard

  • 2
    Just string split on the `=`, `Trim()` and compare as you would normally... – maccettura Oct 26 '18 at 20:34
  • 1
    That looks like an assignment rather than a comparison. – Rufus L Oct 26 '18 at 20:36
  • 1
    Look into using a library for this. FLEE is a good option. https://github.com/mparlak/Flee – Michael G Oct 26 '18 at 20:39
  • 1
    Can you explain why you need this, its very rare that someone actually needs this but rather most of the times someone requests a string parser comparison tool is a lack of knowledge, how to reach the actual result? – Rand Random Oct 26 '18 at 20:40
  • 2
    @RandRandom everything is a string to new programmers `¯\_(ツ)_/¯` – maccettura Oct 26 '18 at 20:40
  • Are you expecting the result of "XYZ = XYZ" to be `'XYZ' == 'XYZ'`, or is `XYZ` a variable, database column name, or some other entity which may have a more interesting value than a string literal? – HABO Oct 26 '18 at 20:41
  • @HABO its a string lieral – Wade Martin Oct 26 '18 at 20:45
  • Does [this](https://stackoverflow.com/questions/51369260/how-to-evaluate-single-string-in-if-c-sharp) question with no details help? We still can't answer it without a (more) complete syntax specification. – HABO Oct 26 '18 at 20:50
  • This isn't a dup. The expression he shows isn't C# and the question he asks has a reasonably simple answer. – Flydog57 Oct 26 '18 at 21:38
  • @Flydog57 - assuming that this is all he wants, since OP is only providing fake data we can't tell for sure if that is all he wants - maybe he needs more, with the given duplication he can do his request (if he switches to == instead of =) and other stuff - but since OP is ignoring questions we may never know what he really wants – Rand Random Oct 26 '18 at 21:45

2 Answers2

0

You don't need a lambda expression. Just Split the string and use string.Equals. Something like:

var input = "XYZ = XYZ";
var split = input.Split('=');

return string.Equals(split[0].Trim(), split[1].Trim());
Lews Therin
  • 3,707
  • 2
  • 27
  • 53
  • This would work however, the string may be dynamic and contain something like "XYZ = XYZ and XYZ != XYaZ" – Wade Martin Oct 26 '18 at 20:36
  • 5
    @WadeMartin please update the question with all the rules and samples – Rufus L Oct 26 '18 at 20:37
  • 1
    @WadeMartin the question is not complete unless you have included all details necessary to answer. – maccettura Oct 26 '18 at 20:38
  • Then you need to parse it as an expression tree, but given you're using = to mean "equals" (vs == as per C# etc.) you can't use the .NET C# compiler, and given you're using != to mean not equals you can't use the VB compiler either - so what language is it? – Dylan Nicholson Oct 26 '18 at 20:39
  • If you want to parse VB-like syntax, https://stackoverflow.com/questions/37670024/can-roslyn-be-used-for-vb-net-scripting might be helpful (but you'd need to replace != with <>) – Dylan Nicholson Oct 26 '18 at 20:45
0

You could use a regex that looks something like this:

 ([a-zA-Z]\w*)\s*(<|>|(={1,2})|!=)\s*([a-zA-Z]\w*)

That will match three things:

  1. a word consisting of an upper or lower case character followed by any number of upper or lower case characters or digits
  2. one of "=", "==", "<" or ">" for comparison
  3. a word with the same definition as #1.

After that, all you need to do is get the matches and groups, look at the two words and look at the comparison type and go from there.

Here's some sample code that uses this:

 private void RegexTest()
 {
     var regex = new Regex(@"([a-zA-Z]\w*)\s*(<|>|(={1,2})|!=)\s*([a-zA-Z]\w*)");
     var inputs = new string[]
     {
         "XYZ = XYZ",
         "ABC != A",
         "1 > 3",
         "a12 < m34"
     };
     foreach (var input in inputs)
     {
         var match = regex.Match(input);
         if (match.Length > 0)
         {
             Debug.WriteLine($"Match for ({input}): First: {match.Groups[1]}, Comparer: {match.Groups[2]}, Second: {match.Groups[4]}");
         }
         else
         {
             Debug.WriteLine($"No match for {input}");
         }
     }
 }

This results in output that looks like:

Match for (XYZ = XYZ): First: XYZ, Comparer: =, Second: XYZ
Match for (ABC != A): First: ABC, Comparer: !=, Second: A
No match for 1 > 3
Match for (a12 < m34): First: a12, Comparer: <, Second: m34
Flydog57
  • 6,851
  • 2
  • 17
  • 18