1

I want to generate conditional code of the string, which will check the string. An example is the following string sampleString = "C123 A091 A111 A122 B120 B309 C000" and examples of conditional string I have is as follows

example 1 : A123 + B123 would I generate as

if sampleString.contains ("A123") And sampleString.contains ("B123") Then
'doSomething
else
'doSomething
end if

example 2 : A111+A122+(B120/-C123)

if sampleString.contains ("A111") And sampleString.contains ("A122") And (sampleString.contains ("B120") Or Not sampleString.contains ("C123")) Then
'doSomething
else
'doSomething
end if
  • Plus (+) means AND
  • Minus (-) means NOT
  • Slash (/) means OR

Will I be able to do this in VB.Net?

svick
  • 236,525
  • 50
  • 385
  • 514
danangaji
  • 13
  • 2
  • 2
    Why would you use this confusing syntax? If anything, use something that makes more sense, like `&`, `!` and `|`. – svick Feb 11 '13 at 02:56
  • @svick My problem is how to convert string to conditional syntax in VB.NET as I wrote before. – danangaji Feb 11 '13 at 03:04
  • 1
    What would you do with this code if you had it? – John Saunders Feb 11 '13 at 03:26
  • http://dragonbook.stanford.edu/ – Brian Webster Feb 11 '13 at 07:20
  • 1
    Yes, of course you can do it in VB.NET, but there is no built-in capability in the language that is provided for you to do that kind of thing. You will need to write your own expression tree parser. Once you have parsed the syntax into a tree, it is very easy to walk the tree and calculate the result. As @BrianWebster points out, parsing expression trees is typically covered in compiler courses. It's really not an overly complicated problem to tackle, but if you are a beginner, you may be biting off more than you can chew. – Steven Doggart Feb 11 '13 at 10:51

1 Answers1

0

Use Regex to solve the Contains problem by finding the matches and replace them with 1 or 0.

Dim sample As String = "C123 A091 A111 A122 B120 B309 C000"
Dim input As String = "A111 +  A122 + ( B120/- C123)"

Dim nRegex As New Regex("\w+", RegexOptions.IgnoreCase)
Dim nMatches As MatchCollection = nRegex.Matches(input)

For Each nMatch As Match In nMatches

    input = input.Replace(nMatch.Value, IIf(sample.Contains(nMatch.Value), "1", "0"))

Next

Now replace the special characters you have with ones that can actually give you the right result when evaluated as a math expression (Since logic is originally math).

input = input.Replace(" ", "")
input = input.Replace("+", "*")
input = input.Replace("/", "+")
input = input.Replace("-1", "0").Replace("-0", "1")

after all this you will get this expression :

1*1*(1+0)

You can now use any math evaluator as suggested in this Answer. If the result you get is bigger than zero means Its logically True. Otherwise False

Community
  • 1
  • 1
Abdusalam Ben Haj
  • 5,343
  • 5
  • 31
  • 45
  • I get the bug if i get math expression like `-(0*0*0)+-(0+0)`. if we use math evaluator it will give the result as `0` (false). – danangaji Jun 07 '13 at 02:39