2012-11-07 39 views
5

Giả sử chúng ta có chuỗi như:Làm thế nào để chuyển đổi một chuỗi thành một đối tượng BinaryExpression?

string s1 = "a < 5"; 
string s2 = "b >= 7"; 
string s3 = "c <= 8"; 
... 

Tôi muốn chuyển đổi chuỗi đó vào BinaryExpression đối tượng tương tự như những gì chúng ta có được bằng cách sử dụng:

BinaryExpression b1 = Expression.MakeBinary(ExpressionType.LessThan, Expression.Parameter(typeof(int), "a"), Expression.Constant(5, typeof(int))); 
    BinaryExpression b2 = Expression.MakeBinary(ExpressionType.GreaterThanOrEqual, Expression.Parameter(typeof(int), "b"), Expression.Constant(7, typeof(int))); 
    BinaryExpression b3 = Expression.MakeBinary(ExpressionType.LessThanOrEqual, Expression.Parameter(typeof(int), "c"), Expression.Constant(8, typeof(int))); 

Tôi đã tạo ra phương pháp dưới đây:

BinaryExpression ConvertStringToBinaryExpression(string exp) 
{ 
    string[] s = exp.Split(' '); 
    string param = s[ 0 ]; 
    string comparison = s[ 1 ]; 
    int constant = int.Parse(s[ 2 ]); 
    if (comparison == "<") 
    return Expression.MakeBinary(ExpressionType.LessThan, Expression.Parameter(typeof (int), param), Expression.Constant(constant, typeof (int))); 
    else if (comparison == "<=") 
    return Expression.MakeBinary(ExpressionType.LessThanOrEqual, Expression.Parameter(typeof (int), param), Expression.Constant(constant, typeof (int))); 
    else if (comparison == ">") 
    return Expression.MakeBinary(ExpressionType.GreaterThan, Expression.Parameter(typeof (int), param), Expression.Constant(constant, typeof (int))); 
    else if (comparison == ">=") 
    return Expression.MakeBinary(ExpressionType.GreaterThanOrEqual, Expression.Parameter(typeof (int), param), Expression.Constant(constant, typeof (int))); 
    else 
    throw new ArgumentException("Invalid expression.", "exp"); 
} 

Tuy nhiên, phương pháp trên không hoạt động bình thường nếu, ví dụ: chúng tôi có các chuỗi như:

string s4 = "a< 5" // no space between 'a' and '<' 
string s5 = "b>=9" // no space at all 
string s6 = "c <=7" // no space betwen '<=' and '7' 

Cách dễ nhất để làm cho nó trở nên mạnh mẽ và đáng tin cậy hơn là gì?

+2

bạn sẽ chỉ sử dụng>, <,=,<=,> =, =>, =

+0

@KrishanuDey: yes –

Trả lời

2

Như Harsha chỉ nó ra regex sẽ làm cho công việc của bạn đơn giản

Match m=Regex.Match("var4 <= 433",@"(?'leftOperand'\w+)\s*(?'operator'(<|<=|>|>=))\s*(?'rightOperand'\w+)"); 
m.Groups["leftOperand"].Value;//the varaible or constant on left side of the operator 
m.Groups["operator"].Value;//the operator 
m.Groups["rightOperand"].Value;//the varaible or constant on right side of the operator 
+0

nhưng điều này sẽ trả về false nếu chuỗi chứa bất kỳ không gian –

+0

đó là y tôi đã sử dụng '\ s *' trong regex để khớp với 0 hoặc nhiều không gian – Anirudha

+0

hoạt động như mong đợi. cảm ơn! –

2

Sử dụng regex để đối sánh với một số loại biểu thức có thể có (nếu bạn có một danh sách ngắn đầy đủ và hợp lý các biểu thức có thể). Cũng phân tích cú pháp chuỗi nếu nó không khớp với regex của bạn để kiểm tra bất kỳ ký tự không mong muốn nào.

EDIT: Regex và phân tích cú pháp sẽ giúp bạn "xóa" chuỗi trước khi bạn có thể sử dụng trường hợp chuyển đổi/nếu-khác của mình.

http://www.c-sharpcorner.com/UploadFile/prasad_1/RegExpPSD12062005021717AM/RegExpPSD.aspx

Các vấn đề liên quan